Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4582816
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T21:07:42+00:00 2026-05-21T21:07:42+00:00

I have a MySQL query to select all product id’s with certain filters applied

  • 0

I have a MySQL query to select all product id’s with certain filters applied to the products. This query
works but I want to learn to improve this query. Alternatives for this query are welcome with explanation.

SELECT kkx_products.id from kkx_products WHERE display = 'yes' AND id in
    (SELECT product_id FROM `kkx_filters_products` WHERE `filter_id` in 
       (SELECT id FROM `kkx_filters` WHERE kkx_filters.urlname = "comics" OR kkx_filters.urlname = "comicsgraphicnovels") 
    group by product_id having count(*) = 2) 
    ORDER BY kkx_products.id desc LIMIT 0, 24 

I’ve included the structure of the tables being used in the query.

EXPLAINkkx_filters;

Field            Type                 Null  Key     Default        Extra
id               int(11) unsigned     NO    PRI     NULL           auto_increment
name             varchar(50)          NO             
filtergroup_id   int(11)              YES   MUL     NULL     
urlname          varchar(50)          NO    MUL     NULL     
date_modified    timestamp            NO            CURRENT_TIMESTAMP    
orderid          float(11,2)          NO            NULL    

EXPLAIN kkx_filters_products;

Field            Type                 Null  Key   Default          Extra
filter_id        int(11)              NO    PRI   0    
product_id       int(11)              NO    PRI   0   

EXPLAIN kkx_products;

Field            Type                 Null  Key   Default          Extra
id               int(11)              NO    PRI   NULL             auto_increment
title            varchar(255)         NO           
urlname          varchar(50)          NO    MUL        
description      longtext             NO          NULL   
price            float(11,2)          NO          NULL   
orderid          float(11,2)          NO          NULL   
imageurl         varchar(255)         NO            
date_created     datetime             NO          NULL    
date_modified    timestamp            NO          CURRENT_TIMESTAMP    
created_by       varchar(11)          NO          NULL    
modified_by      varchar(11)          NO          NULL   
productnumber    varchar(32)          NO           
instock          enum('yes','no')     NO          yes    
display          enum('yes','no')     NO          yes    
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T21:07:43+00:00Added an answer on May 21, 2026 at 9:07 pm

    Instead of using inline queries in your criteria statements, try using the EXISTS block…
    http://dev.mysql.com/doc/refman/5.0/en/exists-and-not-exists-subqueries.html

    You will be able to see the difference in your explain plan. Before you had a query executing for each and every record in your result set, and every result in that inline view result set had its own query executing to.

    You see how nested inline views can create an exponential increase in cost. EXISTS doesn’t work that way.

    Example of the use of EXISTS:

    Consider tbl1 has columns id and data. tbl2 has columns id, parentid, and data.

    SELECT a.*
    FROM tbl1 a
    WHERE 1 = 1 
    AND EXISTS (
      SELECT NULL 
      FROM tbl2 b
      WHERE b.parentid = a.id 
      AND b.data = 'SOME CONDITIONAL DATA TO CONSTRAIN ON'
    )
    

    1) We can assume the 1 = 1 is some condition that equates to true for every record
    2) Doesn’t matter what we select in the EXISTS statment really, NULL is fine.
    3) It is important to look at b.parentid = a.id, this links our exist statement to the result set

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a mysql query like this: $topicreplycount = mysql_query('SELECT COUNT(DISTINCT post_msg_id) FROM phpbb_attachments
So, I have this mysql query: SELECT member_id, text FROM review WHERE text !=
I have a MySQL query like this: SELECT DAYNAME(CreatedAt) AS TheDay, SUM(Amount) AS TheSum
I have a MySQL SQL query that selects products and the text translations: SELECT
I have a MySQL query SELECT * FROM 'redirect' WHERE 'user_id'= \''.$_SESSION['user_id'].' \' ORDER
I have the following MySQL query statement: SELECT * FROM table1 INNER JOIN table2
I have the following mysql query: $manufacturers_query = select m.manufacturers_id, m.manufacturers_name, m.manufacturers_image, mi.manufacturers_url from
I have an application that executes the following MySQL query: SELECT 402 AS user_id,
So i have this script: <?php $query = mysql_query( SELECT meetup AS text, id
i have a problem concerning a select query in MYSQL i have two different

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.