MySQL 大批量插入,如何过滤掉重复数据?
往期热门文章: 1、Spring官方为什么建议构造器注入? 2、还在用 Random生成随机数?试试 ThreadLocalRandom,超好用! 3、这些年 Java8 的 Optional 你用对了吗? 4、当 Docker 遇上 IDEA ,生产力彻底炸裂了 5、如何把Spring Boot的Jar包做成exe?超详细教程来了! 来源:telami.cn/2019/mysql-removes-duplicate -data-and-keeping-only-one/
CREATE TABLE `animal` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
INSERT INTO `pilipa_dds`.`student` (`id`, `name`, `age`) VALUES ('1', 'cat', '12');
INSERT INTO `pilipa_dds`.`student` (`id`, `name`, `age`) VALUES ('2', 'dog', '13');
INSERT INTO `pilipa_dds`.`student` (`id`, `name`, `age`) VALUES ('3', 'camel', '25');
INSERT INTO `pilipa_dds`.`student` (`id`, `name`, `age`) VALUES ('4', 'cat', '32');
INSERT INTO `pilipa_dds`.`student` (`id`, `name`, `age`) VALUES ('5', 'dog', '42');
SELECT name,count( 1 )
FROM
student
GROUP BY
NAME
HAVING
count( 1 ) > 1;
name count(1) cat 2 dog 2
Select * From 表 Where 重复字段 In (Select 重复字段 From 表 Group By 重复字段 Having Count(1)>1)
删除全部重复数据,一条不留
DELETE
FROM
student
WHERE
NAME IN (
SELECT NAME
FROM
student
GROUP BY
NAME
HAVING
count( 1 ) > 1)
1093 - You can't specify target table 'student' for update in FROM clause, Time: 0.016000s
DELETE
FROM
student
WHERE
NAME IN (
SELECT
t.NAME
FROM
( SELECT NAME FROM student GROUP BY NAME HAVING count( 1 ) > 1 ) t)
删除表中删除重复数据,仅保留一条
SELECT
*
FROM
student
WHERE
id NOT IN (
SELECT
t.id
FROM
( SELECT MIN( id ) AS id FROM student GROUP BY `name` ) t
)
开始删除重复数据,仅留一条
DELETE
FROM
student
WHERE
id NOT IN (
SELECT
t.id
FROM
( SELECT MIN( id ) AS id FROM student GROUP BY `name` ) t
)
最近热文阅读:
1、Spring官方为什么建议构造器注入? 2、还在用 Random生成随机数?试试 ThreadLocalRandom,超好用! 3、这些年 Java8 的 Optional 你用对了吗? 4、当 Docker 遇上 IDEA ,生产力彻底炸裂了 5、如何把Spring Boot的Jar包做成exe?超详细教程来了! 6、彻底搞懂 Nginx 的五大应用场景 7、推荐60个相见恨晚的神器工具 8、为什么有些大公司技术弱爆了? 9、这 40 道 Redis 面试题让你不再慌(附答案) 10、优秀的代码都是如何分层的? 关注公众号,你想要的Java都在这里
评论