返回具有共同的特定列但包含相同列值的行

时间:2018-11-05 19:51:19

标签: sql

对不起,我不确定如何正确表达此问题。这是我正在谈论的示例:

#       food   type
1       muffin vegan
2       pasta  vegan     
3       cookie vegan     
4       burger vegan
5       bread  idk
6       boba   idk
7       muffin ok
8       pasta  ok
9       muffin no
10      pasta  yes
11      muffin hi

所以我想返回所有包含相同类型的松饼和面食的行。因此,如果我的查询正确,则将返回:

食物类型

1       muffin vegan
2       pasta  vegan  
3       cookie vegan     
4       burger vegan   
7       muffin ok
8       pasta  ok

我该怎么做?

1 个答案:

答案 0 :(得分:0)

select * from T where type in (
    select type from T
    where food in ('muffin', 'pasta')
    group by type
    having count(distinct food) = 2
);