如何从MySQL表中删除重复的值 [英] how to remove duplicate values from MySQL table

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

问题描述

我正在寻找查询,删除所有重复的值.

i am looking for the query, deletes the all duplicate values.

Example Table:

1 ABC
2 BBB
3 DAC
4 ABC
5 AAA
6 ABC

output required

1 ABC
2 BBB
3 DAC
5 AAA

感谢您的帮助,Google找不到确切的解决方案.

thanks for your help, i Google it can't find exact solution.

推荐答案

如果要对重复值进行实际的DELETE操作(同时保留具有最低id的值),则可以使用多表DELETE语法:

If you want to do an actual DELETE operation of the duplicate values (while retaining the values having the lowest id), you can do it with the multiple table DELETE syntax:

DELETE a FROM tbl a
LEFT JOIN
(
    SELECT MIN(id) AS id, name
    FROM tbl
    GROUP BY name
) b ON a.id = b.id AND a.name = b.name
WHERE b.id IS NULL

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

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