SQL where子句,用于检查所有选定列值是否位于子查询select中

SQL where子句,用于检查所有选定列值是否位于子查询select中,sql,sql-server,Sql,Sql Server,下面的select概念是否可以转换为sql select select S_ID from table1 where S_Type = TYPE and all S_ID in (select S_ID from table2) 其概念如下: 项1、项2和项3都应位于表中的select项中 select语句应该只返回一行/s,如果所有的s_ID都在表2中的select s_ID中,则需要输入比较运算符,然后是all select S_ID from table1 where S_ID = AL

下面的select概念是否可以转换为sql select

select S_ID from table1 where S_Type = TYPE and all S_ID in (select S_ID from table2)
其概念如下:

项1、项2和项3都应位于表中的select项中


select语句应该只返回一行/s,如果所有的s_ID都在表2中的select s_ID中,则需要输入比较运算符,然后是all

select S_ID from table1 where S_ID = ALL (select S_ID from table2)

您需要将比较运算符

select S_ID from table1 where S_ID = ALL (select S_ID from table2)
如果您想要其所有项目都在第二个表中的S_ID,则使用聚合

select t1.S_ID
from table1 t1
where t1.S_Type = 'TYPE' and
      t1.item in (select t2.item from table2 t2)
group by S_ID
having count(distinct t1.item) = (select count(distinct t2.item) from table2 t2);
如果您想要其所有项目都在第二个表中的S_ID,则使用聚合

select t1.S_ID
from table1 t1
where t1.S_Type = 'TYPE' and
      t1.item in (select t2.item from table2 t2)
group by S_ID
having count(distinct t1.item) = (select count(distinct t2.item) from table2 t2);

你能分享一些S_ID列中的数据吗?我不清楚。如果表2中未包含所有S_ID,会发生什么情况?@TimBiegeleisen-请查看我的更新question@onhax . . . 这毫无意义。为什么不从第二个表中选择s_id?什么是项目1等?它们不在查询中。我想我们肯定需要表和预期输出的样本数据。我无法决定您是想要一个简单的内部联接,还是想要一个基于整个行子集的复杂比较。您可以共享S_ID列中的一些数据吗?我不清楚。如果表2中未包含所有S_ID,会发生什么情况?@TimBiegeleisen-请查看我的更新question@onhax . . . 这毫无意义。为什么不从第二个表中选择s_id?什么是项目1等?它们不在查询中。我想我们肯定需要表和预期输出的样本数据。我无法决定您是想要一个简单的内部联接,还是想要一个基于整个行子集的复杂比较。谢谢您的回复。我来测试一下,马上回来告诉你答案。我要测试一下,马上回来