举个例子,有两张表如下:
A
userid val
111 aaa
111 bbb
111 ccc
222 bbb
222 ccc
222 ddd
333 ccc
B
val
bbb
ccc
得到A表里val包含B表全部val的userid
结果
userid
111
222
另外想补充的是A表数据量是亿级别的,尽量要考虑一下性能的问题。
A
userid val
111 aaa
111 bbb
111 ccc
222 bbb
222 ccc
222 ddd
333 ccc
B
val
bbb
ccc
得到A表里val包含B表全部val的userid
结果
userid
111
222
另外想补充的是A表数据量是亿级别的,尽量要考虑一下性能的问题。
解决方案
100
select distinct userid
from A t
where not exists (select 1 from B where not exists (select 1 from A where userid=t.userid and val=b.val ))
from A t
where not exists (select 1 from B where not exists (select 1 from A where userid=t.userid and val=b.val ))