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

来源:互联网 发布:java开源项目管理软件 编辑:程序博客网 时间:2024/05/04 18:57

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 * fromtable)  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 错误。


0 0