查找两个SQL数据表之间的不匹配
我有两个sql数据表,分别命名为查找两个SQL数据表之间的不匹配,sql,sql-server,Sql,Sql Server,我有两个sql数据表,分别命名为tmptable1和tmptable2 我试图显示在tmptable1中但不在tmptable2 我编写了以下查询,但它显示的是空白结果,但我知道有一条记录在tmptable1中,但在tmptable2 下面是我的问题。我做错了什么 select * from tmptable1 where name not in(select name from tmptable2 where status='active') 一种方法是将notexists与相关子查询一起使
tmptable1
和tmptable2
我试图显示在tmptable1
中但不在tmptable2
我编写了以下查询,但它显示的是空白结果,但我知道有一条记录在tmptable1
中,但在tmptable2
下面是我的问题。我做错了什么
select * from tmptable1 where name not in(select name from tmptable2 where status='active')
一种方法是将
notexists
与相关子查询一起使用:
select *
from tmptable1 t1
where not exists (
select 1
from tmptable2 t2
where t1.name = t2.name
and t2.status = 'active'
);
或者,您可以使用左连接
:
select t1.*
from tmptable1 t1
left join tmptable2 t2
on t1.name = t2.name
and t2.status = 'active'
where t2.name is null;
您还可以利用: 下面给出了
tmptable1
中存在但不在tmptable2
中的名称:
SELECT name FROM tmptable1
EXCEPT
SELECT name FROM tmptable2
鉴于此,您可以使用以下通用名称:
SELECT name FROM tmptable1
INTERSECT
SELECT name FROM tmptable2
您好,我使用的是MS SQLcan u,请指定完整的查询示例数据可以是姓名、地址、城市、手机号码和国家/地区