我得到了一个包含名称和IP地址的列表:
ID | Name | Address
1 | Peter | 111.222.333
2 | John | 999.888.333
3 | Hans | 111.222.333
4 | Kimberly | 555.555.1111
我正试图找到一种方法来使用SQL显示重复的帐户:
Name | Second account (found by IP)
Peter | Hans
请帮忙,
谢谢。
答案 0 :(得分:1)
你只需要将表加入自己。
select
t1.name, t2.name as second_account
from
your_table t1
inner join your_table t2 on t1.address = t2.address and t1.name < t2.name
答案 1 :(得分:1)
如果你有两个以上的重复
1 | Peter | 111.222.333
2 | John | 111.222.333
3 | Hans | 111.222.333
fancyPants的查询将返回所有可能的组合,如
Name | Second account (found by IP)
Peter | Hans
Peter | John
John | Hans
这将返回重复列表,而不是对:
select address, name
from tab
where address in
( select address
from tab
group by address
having count(*) > 1
)
根据您的需要,这也可能没问题:
select min(name), max(name), count(*) as number_of_duplicates
from tab
group by address
having count(*) > 1