Selecting against subsets of a list in MySQL

前端 未结 7 1934
谎友^
谎友^ 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:32

    If you pretend that your filter is in a table:

    select * 
    from product p
    where not exists (
        select 1
        from attributes a
        where a.product_id = p.product_id
        and not exists(
            select 1
            from filter f
            where f.id_attribute = a.id_attribute))
    

    If it was in a constructed query:

    select * 
    from product p
    where not exists (
        select 1
        from attributes a
        where a.product_id = p.product_id
        and attribute_id not in ())
    

    This is off the top of my head, so may have typos.

提交回复
热议问题