准备的测试表结构及数据
插入的数据中A,B,E存在重复数据,C没有重复记录
CREATE TABLE `tab` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of tab
-- ----------------------------
INSERT INTO `tab` VALUES ('1', 'A');
INSERT INTO `tab` VALUES ('2', 'A');
INSERT INTO `tab` VALUES ('3', 'A');
INSERT INTO `tab` VALUES ('4', 'B');
INSERT INTO `tab` VALUES ('5', 'B');
INSERT INTO `tab` VALUES ('6', 'C');
INSERT INTO `tab` VALUES ('7', 'B');
INSERT INTO `tab` VALUES ('8', 'B');
INSERT INTO `tab` VALUES ('9', 'B');
INSERT INTO `tab` VALUES ('10', 'E');
INSERT INTO `tab` VALUES ('11', 'E');
INSERT INTO `tab` VALUES ('12', 'E');
使用HAVING关键字筛选出表中重复数据
SELECT `name`,COUNT(1) FROM TAB GROUP BY `name` HAVING COUNT(1) >1
可以通过分组语句从每种重复数据中都拿出一条标识
SELECT `name`,id FROM TAB GROUP BY `name` HAVING COUNT(1) >1
删除重复记录并且只保留一条 [留意SQL注释]
DELETE from tab where
-- 删除所有的重复时间 Begin --
`name` in (
SELECT * from (SELECT `name`FROM TAB GROUP BY `name` HAVING COUNT(1) >1) tmp2
)
-- 删除所有的重复时间 END --
-- 但一些特定ID的记录不进行删除 Begin --
AND
id NOT in(
select id from (
SELECT `name`,id FROM TAB GROUP BY `name` HAVING COUNT(1) >1
) tmp1
)
-- 但一些特定ID的记录不进行删除 END --
执行后最终结果
方法二
方法一查询出的所有多余的重复记录:
方法二查询出的所有多余的重复记录(与方法一的结果相同):
方法三查询出的所有多余的重复记录:这里方法三因为用了MAX()方法(也可改用MIN()),查询结果记录的id不太一样,但也可以被视为重复多余的数据,关键是你希望选择保留哪一条记录而已。
-- 方法一 SELECT * FROM t_user WHERE user_name IN ( SELECT user_name FROM t_user GROUP BY user_name HAVING COUNT(1)>1 ) AND id NOT IN ( SELECT MIN(id) FROM t_user GROUP BY user_name HAVING COUNT(1)>1 )
-- 方法二 SELECT * FROM t_user WHERE id NOT IN ( SELECT MIN(id) FROM t_user GROUP BY user_name )
-- 方法三 SELECT * FROM t_user AS t1 WHERE t1.id <> ( SELECT MAX(t2.id) FROM t_user AS t2 WHERE t1.user_name=t2.user_name )
这里方法三因为用了MAX()方法(也可改用MIN()),查询结果记录的id不太一样,但也可以被视为重复多余的数据,关键是你希望选择保留哪一条记录而已。
-- 方法一(笨方法但容易理解) DELETE FROM t_user WHERE user_name IN ( SELECT t1.user_name FROM ( -- 查询出所有重复的user_name SELECT user_name FROM t_user GROUP BY user_name HAVING COUNT(1)>1 ) t1 ) AND id NOT IN ( SELECT t2.min_id FROM ( -- 查询出所有重复的记录并各自只取其中一条(MIN(id)或MAX(id)都可以) SELECT MIN(id) AS min_id FROM t_user GROUP BY user_name HAVING COUNT(1)>1 ) t2 )
-- 方法二(推荐方法也容易理解) DELETE FROM t_user WHERE id NOT IN ( SELECT t.min_id FROM ( -- 过滤出重复多余的数据,比如,如果所有记录中存在1条记录是user_name=zhangsan的,那么就取出它; -- 如果所有记录中存在多条记录是user_name=lisi的,那么只取其中1条,其他的不查询出来 SELECT MIN(id) AS min_id FROM t_user GROUP BY user_name ) t )
-- 方法三(推荐方法但不太容易理解) DELETE FROM t_user WHERE id IN ( SELECT t.id FROM ( -- 1. 关于所有存在相同user_name的记录,只查询出(保留)重复记录中的1条,假设这样查询出来的集合为A集合。 -- 2. 在所有记录中,只要id不在A集合中的,都把它们查询出来 SELECT t1.id FROM t_user AS t1 WHERE t1.id <> (SELECT MAX(t2.id) FROM t_user AS t2 WHERE t1.user_name=t2.user_name) ) t ) -- 或 DELETE FROM t_user t1 WHERE t1.id <> ( SELECT t2.max_id FROM ( SELECT MAX(t3.id) AS max_id FROM t_user t3 WHERE t1.user_name=t3.user_name ) t2 )
参考:
如何实现 MySQL 中通过SQL语句删除重复记录并且只保留一条记录_51CTO博客_删除重复记录的sql