Question

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    

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top