Re: [SQL ] 刪除重複資料
※ 引述《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
: 謝謝!
另外建一個一模一樣的Table,假設為B表,你本來的叫A表
則用
1. Insert into B (id ,username,phone)
(select id,username,phone from A group by phone)
2. delete from A
3. Insert into A (id,username,phone) (select id ,username,phone from B)
三步解決!
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 220.134.154.61
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 3 篇):