Selecting against subsets of a list in MySQL

前端 未结 7 1936
谎友^
谎友^ 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条回答
  •  旧时难觅i
    2021-01-12 07:50

    select
        P.id,
        P.name,
        count(P.id) as matched_attr_count,
        count(PA.a_id) as total_attr_count
    from
        product_attributes PA
        left join product P on P.id = PA.p_id and PA.a_id in (21,23,24)
    group by
        PA.p_id
    having
        matched_attr_count = total_attr_count;
    

提交回复
热议问题