SQL重复电话查询

时间:2014-01-23 10:18:27

标签: sql sql-server database nested-queries

我有两个带有电话号码的表,我想运行查询以检查两个表中是否有任何重复的电话号码。这些表称为class1和class2我提出了以下查询:

Select * 
from Class1 
where Telephone in (select 
               Telephone 
               from Class2
               group by Telephone
               having count(*) > 1)

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:2)

你混合了两件事:

Select * from Class1 where Telephone in (select Telephone from Class2);

为您提供两个表格中的所有数字。

Select Telephone from Class1 group by Telephone having count(*) > 1;

为您提供Class1中的所有重复数字。

您可以将这些结合起来:

Select 'Class1' as whattable, Telephone from Class1 group by Telephone having count(*) > 1
union all
Select 'Class2' as whattable, Telephone from Class2 group by Telephone having count(*) > 1
union all
Select 'both' as whattable, Telephone from Class1 where Telephone in (select Telephone from Class2);