在日常日常應用中,經常需要查詢出數據表中重複的數據,以便對其進行刪除等操作。以下是T-SQL查詢數據表中重複數據的幾種方法:
例如 表test_a
id name
1 aa
2 bb
3 cc
4 dd
5 ee
8 aa
9 ee
查詢結果:
1 aa
2 bb
3 cc
4 dd
5 ee
方法1:
select A.* from test_a as A where id not in(select min(id) as oldid from test_a group by name)
方法2:
select * from test_a where not id in(select max(id) from test_a group by name having count(name)>1)