作者artarmon (Artarmon)
看板Database
标题Re: [SQL ] 删除重复资料
时间Fri Mar 21 20:48:36 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
: 谢谢!
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