MySQL删除重复的连续行 [英] MySQL Delete duplicates in consecutive rows

查看:189
本文介绍了MySQL删除重复的连续行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设这个表:

ID ColA ColB
1   7    8
2   7    9
3   7    9
4   5    8
5   6    9
6   6    9
7   5    4

PK是ID列。
现在,我想删除连续行中 ColA ColB 的所有重复项。

The PK is the ID coumn. Now, I want to delete all duplicates of ColA and ColB in consecutive rows.

在此示例中,第2,3和5,6行包含重复项。
这些将被删除,以便保持较高的ID。

In this example rows 2,3 and 5,6 contain duplicates. These shall be removed so that the higher ID is remained.

输出应该是:

ID ColA ColB
1   7    8

3   7    9
4   5    8

6   6    9
7   5    4

如何用mySQL完成?

How can this be done with mySQL?

谢谢,
Juergen

Thanks, Juergen

推荐答案

select ID from MyTable m1 where 0 < (select count(*) from MyTable m2 where m2.ID = m1.ID - 1 and m2.ColA = m1.ColA and m2.ColB = m1.ColB)

然后您可以使用MyTable中的一个

and then you can use a

delete from MyTable where ID in ...

查询。这样就可以在任何版本中工作。

query. This way it would surely work in any version.

这篇关于MySQL删除重复的连续行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆