看板 Database 關於我們 聯絡資訊
請問各位 我有一個資料表如下: 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 謝謝! -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 220.135.172.127
bellboy:抱歉補充一下,就是想讓上面0928xxx的資料刪到剩一筆 :) 03/14 09:47
gargamel:這樣刪蠻怪的.反過來說吧,你想保留哪一筆? 03/14 10:35
bellboy:我想保留id最大的那一筆(5)就好,其它1,2,3筆要刪掉的 03/14 10:57
KCTONY:撈出要留下的的id 然後 delete ... where not in ( xxxx ) 03/14 13:15