繁体   English   中英

如果另一个表中存在字段,如何排除行? (SQL Server)

[英]How to exclude row if field exists in another table? (SQL Server)

如果该行中的某个字段存在于另一个表中,则无法排除查询中的行。

例如,

SELECT column1, column2

FROM Table1

假设结果是

**column1**      **column2**
   Name1            Name2 

然后在表2中,还有一列1,它也具有Name1值。

我该如何编辑我的第一个查询以检查Table2,以及它在column1下是否也具有Name1,然后排除整个行?

感谢您的协助!

我会使用not exists

select t1.*
from table1 t1
where not exists (select 1
                  from table2 t2
                  where t2.column1 = t1.column1
                 );

我认为使用not in非常简单易读

SELECT t1.column1, t1.column2 FROM Table1 t1
WHERE t1.column1 NOT IN (SELECT DISTINCT t2.column1 FROM Table2 t2)

暂无
暂无

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

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