this is my table test

这是我的表测试

id   identifier
---  ---------
1      zz
1      zzz
3      d 
5      w
7      v
8      q
9      cc
9      ccc

here I want to remove the duplicate id's and keep the latest id's. the identifier can have duplicate values it dose not matter but the id's should be unique.

在这里,我想删除重复的ID并保留最新的ID。标识符可以有重复的值,这不重要,但id应该是唯一的。

I wrote this query to solve this problem but the problem is that it goes into a infinite loop.

我写了这个查询来解决这个问题,但问题是它进入了无限循环。

please help me with this as I am not able to see the error. Thanks

请帮助我,因为我无法看到错误。谢谢

 delete test
    from test
    inner join(
    select max(id) as lastId, identifier
    from test
    where id in (
              select id 
                from test
               group by id
              having count(*) > 1
       )
    group by id
    )dup on dup.id = test.id
    where test.id<dup.id

4 个解决方案

#1


0

If you have an index on test(id, identifier), the following should be pretty efficient:

如果你有一个关于test(id,identifier)的索引,那么下面应该非常有效:

delete from test
    where test.identifer < (select maxid 
                            from (select max(identifier) as maxid from test t2 where t2.id = t.id
                                 ) a
                           )

The double nested query is a MySQL trick for referencing the update/delete table in the same query.

双嵌套查询是用于在同一查询中引用更新/删除表的MySQL技巧。

更多相关文章

  1. 使用ng-repeat-start和ng-repeat-end与嵌套转发器
  2. 嵌套水平滑块与单击控件
  3. Python2基础用法(中文编码,标识符,注释)
  4. Python(名称空间、函数嵌套、函数对象)
  5. pandas - 将嵌套字典值映射到dataframe列
  6. 如何在sql server中生成并手动插入一个惟一标识符?
  7. android listview多视图嵌套多视图
  8. 解决ScrollView嵌套RecyclerView时item显示不全的问题
  9. 从Wordpress API JSON响应反序列化嵌套的JSON标记和附件

随机推荐

  1. 如何在刀片服务器的foreach循环中插入表
  2. 真人演示乳房按摩法 咪咪大而不垂
  3. 如何在动态创建的HTML标签内容中避免单引
  4. HTML+CSS实现审核流程步骤效果
  5. 使用下一个div中的文本设置“下一个”按
  6. 关注“飞翔的键盘” 近期PlayBook/QNX开
  7. Checkbox CSS帮助或IE6黑客攻击
  8. 更改在表Django中选择了其中一个按钮时显
  9. 关于谱聚类spectral clustering转载自htt
  10. HTML中使用Ajax进行局部刷新页面