查询重复值的两个表

时间:2012-08-09 14:34:51

标签: sql sql-server

我有一个存储过程,可以将记录从一个表移动到另一个表。但是,我现在想要创建一个过程来检查'TableA'中的数据与'TableB'中的数据

SELECT A.Num, B.Num
FROM TableA AS A 
LEFT JOIN TableB AS B ON A.Num = B.Num
WHERE B.Num IS NULL

基本上,我想提取任何不在'TableB'但是在'TableA'中的数字,这是一种LEFT JOIN方式吗?到目前为止,我找不到丢失的文件是不成功的,我已经删除了一些以形成测试用例。

5 个答案:

答案 0 :(得分:7)

您可以使用not exists

SELECT  *   
FROM TableA A  
WHERE NOT EXISTS (SELECT *      
                  FROM TableB B
                  WHERE A.NUM = B.NUM);

not in

SELECT  *   
FROM TableA A  
WHERE A.NUM not in (SELECT B.NUM
                  FROM TableB B);

答案 1 :(得分:3)

SELECT Num from TableA
EXCEPT
SELECT Num from TableB

答案 2 :(得分:2)

select * from tableA
where id not in (select id from tableB)

select * from tableA a
left join tableB b on a.id = b.id
where b.id is null

答案 3 :(得分:1)

查找两个表之间的重复项
select table1.column_name, count(*) from table_name table1
inner join table_name table2
ON table1.coumn_name = table2.column_name;
前任:
SELECT u.username  FROM users u
INNER JOIN src_users ud
ON u.username = ud.username;
在单个表中查找重复项
select username, count(*) from users group by username having count(*) >1;

答案 4 :(得分:0)

select * from tableA
where id in (select id from tableB);