作者jameswiki (乌龟(弄论文中..))
看板Database
标题Re: [SQL ] 删除重复资料
时间Sat Mar 15 14:49:13 2008
※ 引述《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