简体   繁体   English

SQL where 子句检查所有选定的列值是否在子查询中 select

[英]SQL where clause to check if all selected column value is in a subquery select

Does the select concept below can be translated into an sql select?下面的select概念是否可以翻译成sql select?

select S_ID from table1 where S_Type = TYPE and all S_ID in (select S_ID from table2)

The concept of this is like below:这个的概念如下:

item1, item2, and item3 should all be in (select ITEMS from table) item1、item2 和 item3 都应该在(从表中选择项目)

The select statement should only return a row/s if all S_ID is in (select S_ID from table2) select 语句只应在所有 S_ID 都在的情况下返回一行(从表 2 中选择 S_ID)

you need to put comparision operator then all你需要把比较运算符然后全部

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

If you want S_ID s all of whose items are in the second table, then use aggregation如果您想要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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM