Selecting against subsets of a list in MySQL

前端 未结 7 1956
谎友^
谎友^ 2021-01-12 07:21

I\'m quite a begginer and I have two tables: \"product\" and \"product attributes\".

Here\'s some imaginary data (the actual stuff involves more tables )

Pr

7条回答
  •  滥情空心
    2021-01-12 07:38

    This should return only those id's where all attributes for each id are completely contained within the list:

    select attribute_match.id_product from
     (select id_product, count(*) c from attributes
      where id_attribute in (21, 10, 25)
      group by id_product) attribute_match,
     (select id_product, count(*) c_count from attributes
      group by id_product) attribute_total
    where attribute_match.id_product = attribute_total.id_product
          and attribute_match.c = attribute_total.c
    

提交回复
热议问题