Mysql重复行(使用2列检测到重复)

编程入门 行业动态 更新时间:2024-10-18 03:22:33
本文介绍了Mysql重复行(使用2列检测到重复)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何删除此设置中的重复项?

How to remove duplicated in this setup?

id A B ---------------- 1 apple 2 2 orange 1 3 apple 2 4 apple 1

在这里,我想删除两次的(apple,2). ID号是唯一的.如果没有,我将使用DISTINCT关键字.我可以通过A列和B列创建一个键,然后使用DISTINCT关键字获得所需的内容吗?非常感谢您的答复.

In here I want to remove (apple,2) which occurs twice. The id numbers are unique. I would use DISTINCT keyword if it were not. Can I some how make a key out of columns A and B and then use the DISTINCT keyword on that to get what I need ? Many thanks for your replies.

推荐答案

delete from myTable where id not in (select min(id) from myTable group by A, B)

即括号中的select返回A和B的每个分组的第一个ID;删除不在此集合中的所有ID,将删除A-B组合首次出现后的所有出现.

i.e. the select in brackets returns the first id for each grouping of A and B; deleting all ids that are not in this set will remove all occurences of an A-plus-B combination that are "subsequent" to its first occurrence.

编辑:此语法似乎有问题:请参见错误报告:

EDIT: this syntax seems to be problematic: see bug report:

bugs.mysql/bug.php?id=5037

可能的解决方法是:

delete from myTable where id not in ( select minid from (select min(id) as minid from myTable group by A, B) as newtable )

更多推荐

Mysql重复行(使用2列检测到重复)

本文发布于:2023-11-29 20:19:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1647521.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:检测到   Mysql

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!