select 将其所有关联项目匹配到列表的行组

select group of rows that matches all its associated items to a list

我得到了一个 table:

id|name
------
1|Tom
1|Lucy
1|Frank
2|Lucy
2|Frank
3|Frank
4|Jane
5|Robert
6|John

需要 id 的结果,其中与特定 id 关联的所有名称都与列表匹配

列表是('Lucy','Frank','Jane','Robert','Brandon').

我试过这个:

select id 
from table 
group by id 
having name in('Lucy','Frank','Jane','Robert','Brandon')

得到的结果:

1
2
3
4
5

期望的结果:

2
3
4
5

排除的内容:count(distinct id) > 2000 该列表包含 200 个名称,每个 ID 至少与 1 个名称相关联

您可以使用聚合和 having 子句确保组中 name 的 none 不属于列表:

select id
from mytable
group by id
having max(name not in ('Lucy', 'Frank', 'Jane', 'Robert', 'Brandon')) = 0