在实际工作开发过程中,常常会遇到数据库表中存在多条数据重复了,此时我们需要删除重复数据,只保留其中一条有效的数据; 针对这种场景,我们用sql语句该怎么实现呢? 数据准备 建表语句: DROP TABLE IF EXISTS `te
在实际工作开发过程中,常常会遇到数据库表中存在多条数据重复了,此时我们需要删除重复数据,只保留其中一条有效的数据;
针对这种场景,我们用sql语句该怎么实现呢?
建表语句:
DROP TABLE IF EXISTS `test`;CREATE TABLE `test` ( `id` int(11) NULL DEFAULT NULL, `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;INSERT INTO `test` VALUES (1, '张三');INSERT INTO `test` VALUES (2, '李四');INSERT INTO `test` VALUES (4, '张三');INSERT INTO `test` VALUES (5, '王二');INSERT INTO `test` VALUES (6, '护具');INSERT INTO `test` VALUES (7, '无极');INSERT INTO `test` VALUES (8, '护具');INSERT INTO `test` VALUES (3, '空气');INSERT INTO `test` VALUES (9, '王二');INSERT INTO `test` VALUES (10, '几乎');commit;
select t.name,count(1) from test t where 1=1 group by t.name ;
使用having语句进行筛选
select t.name,count(1) from test t where 1=1 group by t.name HAVING count(1) >1;
对于重复数据,保留一条数据筛选
select t.name,min(id) as id ,count(1) from test t where 1=1 group by t.name;
delete from test where id not in (select min(id) from test t where 1=1 group by t.name ) ;
执行上述SQL语句,发现会报错:
delete from test where id not in (select min(id) from test t where 1=1 group by t.name )> 1093 - You can't specify target table 'test' for update in FROM clause> 时间: 0.004s
导致这一原因的问题是:不能在同一表中查询的数据作为同一表的更新数据。
正确参考SQL:
(1) 创建一张表temp_table存储最终保留的数据。create table temp_table as SELECT min( id ) as id FROM test t WHERE 1 = 1 GROUP BY t.NAME;(2) 排除表temp_table中的数据,删除即可。DELETE FROM test WHERE id NOT IN (SELECT * FROM temp_table);
成功删除重复数据!
来源地址:https://blog.csdn.net/weixin_43759352/article/details/132570809
--结束END--
本文标题: MySQL怎样删除重复数据,只保留一条?
本文链接: https://www.lsjlt.com/news/383180.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-04-30
2024-04-30
2024-04-30
2024-04-30
2024-04-30
2024-04-30
2024-04-30
2024-04-30
2024-04-30
2024-04-30
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0