MySQL search products with their attributes

前端 未结 1 1424
臣服心动
臣服心动 2021-01-13 22:12

I have a MySQL database that contains products with their group/attributes/ i created a sample of data in

http://sqlfiddle.com/#!2/7d8a04/1

create ta         


        
相关标签:
1条回答
  • 2021-01-13 22:46

    You need to join with product_filters separately for each attribute:

    SELECT DISTINCT products.*  
    FROM products
    JOIN product_filters AS f1 ON f1.product_id=products.id
    JOIN product_filters AS f2 ON f2.product_id=products.id
    WHERE ( f1.attribute_id=1 and f1.filter_id in (1,2) )
    AND ( f2.attribute_id=3 and f2.filter_id in (6) )
    

    DEMO

    Your version tried to find a single row in product_filters that has both attribute IDs, which isn't possible.

    0 讨论(0)
提交回复
热议问题