如何从两个SQL表中获取不匹配的记录?

时间:2009-07-08 06:52:31

标签: sql

我想从SQL中的两个表中获取不匹配的记录,表结构如下:

表1

Id      Name
1       Prashant
2       Ravi
3       Gaurav
5       Naween
7       Sachin

表2

Id      Name
1       Prashant
2       Ravi
4       Alok
6       Raja

我想要的输出是

Id      Name
3       Gaurav
4       Alok
5       Naween
6       Raja
7       Sachin

在SQL中获取所需输出的查询是什么?

11 个答案:

答案 0 :(得分:9)

我认为joeslice的答案只会给出一半的结果。你需要联合另一个表。或者,您可以执行完全外部联接。

select a.Id, a.Name from Table1 a left outer join Table2 b on a.Name = b.Name where b.Id is null
UNION ALL
select a.Id, a.Name from Table2 a left outer join Table1 b on a.Name = b.Name where b.Id is null

答案 1 :(得分:5)

create table #t1 (Id int, name varchar(50)) 
create table #t2 (Id int, name varchar(50)) 

insert #t1 values (1,       'Prashant')
insert #t1 values (2,      'Ravi')
insert #t1 values (3,       'Gaurav')
insert #t1 values (5,       'Naween')
insert #t1 values (7,       'Sachin')

insert #t2 values (1,       'Prashant')
insert #t2 values (2,       'Ravi')
insert #t2 values (4,       'Alok')
insert #t2 values (6,       'Raja')

select isnull(#t1.id, #t2.id), isnull(#t1.name,#t2.name)  from #t1 
full outer join #t2 on #t1.id = #t2.id
where #t2.Id is null or #t1.id is null

结果:

3   Gaurav
5   Naween
7   Sachin
4   Alok
6   Raja

答案 2 :(得分:5)

Select Id, Name
from Table1
where Id not in (select Id from Table2)
UNION 
Select Id, Name
from Table2
where Id not in (select Id from Table1)

答案 3 :(得分:3)

您实际上可以使用一个查询来执行此操作:

SELECT      COALESCE(table1.ID, table2.ID) AS ID,
            COALESCE(table1.Name, table2.Name) AS Name
FROM        table1
FULL JOIN   table2
        ON  table1.ID = table2.ID
WHERE       table1.ID IS NULL OR table2.ID IS NULL

将导致:

ID          Name
----------- --------
3           Gaurav
5           Naween
7           Sachin
6           Raja
4           Alok

答案 4 :(得分:2)

SELECT * FROM 
(
  SELECT * FROM Table1
 MINUS
  SELECT * FROM Table2
)
UNION
(
  SELECT * FROM Table2
 MINUS
  SELECT * FROM Table1
)

SELECT * FROM
 Table1 a 
FULL OUTER JOIN
 Table2 b 
ON
 a.ID=b.ID AND a.Name=b.NAME
WHERE
 a.ID IS NULL OR b.ID IS NULL

答案 5 :(得分:2)

    select t.ID, t.Name from Table1 t
    left outer join Table2 tt
    ON tt.name = t.name
    where tt.id is null

    union all

    select t.ID, t.Name from Table2 t
    left outer join Table1 tt
    ON tt.name = t.name
    where tt.id is null

答案 6 :(得分:1)

select a.Id, a.Name 
from Table1 a 
left outer join Table2 b 
on a.Name = b.Name 
where b.Id is null

编辑:我的答案是错误的....它只会给你表1中未找到的Table1结果。谢谢你指出了这一点。

答案 7 :(得分:1)

由于您希望从两个表中获取不匹配的记录,我认为您将需要两个查询(每个表一个),这些查询将联合在一起:

(SELECT t1.Id, t1.Name 
 FROM Table1 as t1 
 LEFT OUTER JOIN Table2 as t2 on t1.Name = t2.Name 
 WHERE t2.Id is null)
UNION
(SELECT t2.Id, t2.Name 
 FROM Table2 as t2 
 LEFT OUTER JOIN Table1 as t1 on t2.Name = t1.Name 
 WHERE t1.Id is null)

答案 8 :(得分:0)

SELECT COALSCE(t1.name, t2.name) AS "name"
FROM table1 t1 FULL OUTER JOIN table2 t2
ON t1.id = t2.id
WHERE t1.name IS NULL
OR t2.name IS NULL;

答案 9 :(得分:0)

SELECT COALESCE(t1.name,t2.name) FROM table1 t1全外联接table2 t2 开启t1.id = t2.id 减去 SELECT合并(t1.name,t2.name) 从table1 t1 INNER JOIN table2 t2 开启t1.id = t2.id;

答案 10 :(得分:0)

首先合并两个表:

image/png

临时临时输出:

.png

现在从数据中使用内部联接删除公共ID: 解决方案

with data as
(
select * from t1
union
select * from t2)
select * from data 

最终输出:

    Id  name
1   1   Prashant
2   2   Ravi
3   3   Gaurav
4   4   Alok
5   5   Naween
6   6   Raja
7   7   Sachin