我有这张桌子
----------------
| X | Y |
----------------
| a | 1 |
| c | 6 |
| e | 3 |
| d | 6 |
| c | 4 |
| b | 1 |
| a | 5 |
| g | 1 |
----------------
Run Code Online (Sandbox Code Playgroud)
当我给出一个数组[c,d]时,我需要在上表中找到"6".即对于每一组元素,我需要找到集合中所有元素共享的Y值,但前提是没有其他元素(即不在给定数组中的元素)共享该值.数组中的元素数量没有理论限制.
更多例子:对于[a,b,c],我不需要找到任何东西.对于[a,b]我也不需要找到任何东西(因为g也有一个Y = 1的条目,所以对于[a,b,g]我需要找到"1").
我当然可以遍历数组,按查询查询,但这似乎是一种低效的方式.在SQL中执行此操作的最佳方法是什么?谢谢.
这是一种将“查询”值放在单独的表中的方法。
create table t ( x varchar(1), y int);
insert into t (x, y) values ('a', 1);
insert into t (x, y) values ('c', 6);
insert into t (x, y) values ('e', 3);
insert into t (x, y) values ('d', 6);
insert into t (x, y) values ('c', 4);
insert into t (x, y) values ('b', 1);
insert into t (x, y) values ('a', 5);
insert into t (x, y) values ('g', 1);
create table q ( x varchar(1) );
insert into q (x) values ('a');
insert into q (x) values ('b');
select a.y from
(
select t.y
from t join q on (t.x = q.x)
group by t.y
having count(*) = (select count(*) from q)
) a
join t on (a.y = t.y)
group by a.y
having count(*) = (select count(*) from q)
Run Code Online (Sandbox Code Playgroud)
这假设您不能有重复的组合。
如果您想在没有第二个表的情况下执行此操作,则可以将 替换select count(*)为 IN 列表中匹配的值的数量,并且不要在内部子查询上进行联接,而是使用 where 子句。
select a.y from
(
select t.y
from t
where t.x in ('c', 'd')
group by t.y
having count(*) = 2
) a
join t on (a.y = t.y)
group by a.y
having count(*) = 2
Run Code Online (Sandbox Code Playgroud)