查詢數據表中的重複數據

        在日常日常應用中,經常需要查詢出數據表中重複的數據,以便對其進行刪除等操作。以下是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)

發佈了18 篇原創文章 · 獲贊 3 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章