Re: [SQL ] 刪除重複資料

看板Database作者 (Artarmon)時間16年前 (2008/03/21 20:48), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串3/3 (看更多)
※ 引述《bellboy (鈴鐺人)》之銘言: : 請問各位 : 我有一個資料表如下: : id username phone : ------------------------------- : 1 aaaa 0928xxxxxx : 2 aabb 0928xxxxxx : 3 aacc 0928xxxxxx : 4 1234 0911xxxxxx : 5 aadd 0928xxxxxx : 我想要知道哪些電話號碼是重複的,所以我用group by的方式: : select count(id) as counter, phone from TABLE group by phone having : counter>1 : 然後我想要把多餘的資料刪除,請問我應該要怎麼樣用一句查詢就達成呢? : 我的資料庫是MySQL 5 : 謝謝! with dups as ( select *, row_number() over(partition by phone order by id desc ) as rownum from Table ) delete from Dups where rownum > 1; 我沒用過 MySQL 5,如果有支援 CTE,就可以用上述語法 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 122.105.124.103
文章代碼(AID): #17uwwbdS (Database)
文章代碼(AID): #17uwwbdS (Database)