Sql 如何检查多个表中是否存在列值?
我有4个表,我的第一个表包含10条记录,我喜欢检查这10条记录是否存在于其他表中,并设置一个是或否条件,所有这些表都有一个共享列,即col1,类似这样的Sql 如何检查多个表中是否存在列值?,sql,sql-server,Sql,Sql Server,我有4个表,我的第一个表包含10条记录,我喜欢检查这10条记录是否存在于其他表中,并设置一个是或否条件,所有这些表都有一个共享列,即col1,类似这样的 Table1/Col1 Table2/col1 Table3/col1 Table4/col1 1 Yes No Yes 2 Yes Yes No 3 4 5 6 . . 您可以在案例表达式中使用
Table1/Col1 Table2/col1 Table3/col1 Table4/col1
1 Yes No Yes
2 Yes Yes No
3
4
5
6
.
.
您可以在
案例
表达式中使用exists
:
select t1.col1,
(case when exists (select 1 from table2 t2 where t2.col1 = t1.col1) then 'Yes' else 'No' end) as in_table2,
(case when exists (select 1 from table3 t3 where t3.col1 = t1.col1) then 'Yes' else 'No' end) as in_table3,
. . .
from table1 t1;
您可以在
案例
表达式中使用exists
:
select t1.col1,
(case when exists (select 1 from table2 t2 where t2.col1 = t1.col1) then 'Yes' else 'No' end) as in_table2,
(case when exists (select 1 from table3 t3 where t3.col1 = t1.col1) then 'Yes' else 'No' end) as in_table3,
. . .
from table1 t1;