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
的所有重复项。
在此示例中,第 2,3 行和 5,6 行包含重复项。 这些应被删除,以便保留较高的 ID。
输出应该是:
ID ColA ColB
1 7 8
3 7 9
4 5 8
6 6 9
7 5 4
How can this do with mySQL?
谢谢, 于尔根
Suppose this table:
ID ColA ColB
1 7 8
2 7 9
3 7 9
4 5 8
5 6 9
6 6 9
7 5 4
The PK is the ID coumn.
Now, I want to delete all duplicates of ColA
and ColB
in consecutive rows.
In this example rows 2,3 and 5,6 contain duplicates.
These shall be removed so that the higher ID is remained.
The output should be:
ID ColA ColB
1 7 8
3 7 9
4 5 8
6 6 9
7 5 4
How can this be done with mySQL?
Thanks,
Juergen
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
然后你可以使用
查询。这样它肯定可以在任何版本中工作。
and then you can use a
query. This way it would surely work in any version.
根据您拥有的记录数量,这可能不是最有效的:
可能存在语法错误,因为我通常使用 mssql,但想法应该类似。
Depending on how many records you have, this might not be the most efficient:
There might be syntax errors because I usually use mssql, but the idea should be similar.
我将第一个表称为“测试”。
首先创建一个表来保存 ColA 和 ColB 的所有相同组合:
现在,为 ColA 和 ColB 的每个相同组合选择原始表中的最大 id。将其放入一个新表中(称为 idsToKeep,因为这些是我们不想删除的行):
最后,从原始表中删除所有不在 idsToKeep 表中的条目:
I've called the first table 'test'.
Firstly create a table that will hold all the identical combinations of ColA and ColB:
Now, select the maximum id in the original table for each identical combination of ColA and ColB. Put this into a new table (called idsToKeep because these are the rows we do not want to delete):
Finally, delete all the entries from the original table that are not in the idsToKeep table: