您的位置:首页 > 数据库 > MySQL

mysql数据库 删除重复项 保留ts最新的项

2016-07-06 15:29 274 查看
sql语句是:

delete from table

where key in (select a.
key key from (select * from
table) a where niandu='2016' group  by a. key   having  count(a.key) > 1)

and ts not in (select max(b.ts) from  (select * from
table)  b group by b.key having count(b.key )>1)

其中

 (select * from table) a 是为了在将table当成是子集,防止you cant specify  target table for update in from clause 错误。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  删除重复项 保留