MySQL中如何删除表重复记录

发布时间:2021-08-02 16:22:17 作者:Leah
来源:亿速云 阅读:145

本篇文章给大家分享的是有关MySQL中如何删除表重复记录,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

创建实验表student表数据:
mysql> use test
Database changed
mysql> create table student (id int,name varchar(10));
Query OK, 0 rows affected (1.67 sec)

mysql> insert into student values (11,'aa');
Query OK, 1 row affected (0.26 sec)


mysql> insert into student values (12,'aa');
Query OK, 1 row affected (0.07 sec)


mysql> insert into student values (13,'aa');
Query OK, 1 row affected (0.12 sec)


mysql> insert into student values (14,'aa');
Query OK, 1 row affected (0.11 sec)


mysql> insert into student values (15,'bb');
Query OK, 1 row affected (0.19 sec)


mysql> insert into student values (16,'bb');
Query OK, 1 row affected (0.14 sec)


mysql> insert into student values (17,'cc');
Query OK, 1 row affected (0.15 sec)


mysql> select * from student;
+------+------+
| id   | name |
+------+------+
|   11 | aa   |
|   12 | aa   |
|   13 | aa   |
|   14 | aa   |
|   15 | bb   |
|   16 | bb   |
|   17 | cc   |
+------+------+
7 rows in set (0.22 sec)


方法1:
mysql>  create temporary table temp as select min(id),name from student group by name;
Query OK, 3 rows affected (0.18 sec)
Records: 3  Duplicates: 0  Warnings: 0


mysql> truncate table student;
Query OK, 0 rows affected (0.40 sec)


mysql> insert into student select * from temp;
Query OK, 3 rows affected (0.11 sec)
Records: 3  Duplicates: 0  Warnings: 0


mysql> select * from student;
+------+------+
| id   | name |
+------+------+
|   11 | aa   |
|   15 | bb   |
|   17 | cc   |
+------+------+
3 rows in set (0.00 sec)


mysql> drop temporary table temp;
Query OK, 0 rows affected (0.17 sec)


方法2:
mysql> create temporary table temp as select min(id) as MINID from student group by name;
Query OK, 3 rows affected (0.24 sec)
Records: 3  Duplicates: 0  Warnings: 0


mysql> delete from student where id not in (select minid from temp);
Query OK, 4 rows affected (0.07 sec)


mysql> select * from student;
+------+------+
| id   | name |
+------+------+
|   11 | aa   |
|   15 | bb   |
|   17 | cc   |
+------+------+
3 rows in set (0.00 sec)


方法3:
mysql> delete from student where id not in (select minid from (select min(id) as minid from student
group by name) b);
Query OK, 4 rows affected (0.19 sec)


mysql> select * from student;
+------+------+
| id   | name |
+------+------+
|   11 | aa   |
|   15 | bb   |
|   17 | cc   |
+------+------+
3 rows in set (0.00 sec)


以上就是MySQL中如何删除表重复记录,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

推荐阅读:
  1. MySQL中删除重复记录
  2. Oracle如何删除表中重复记录

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

mysql

上一篇:hbase中创建表、插入数据,更新数据,删除数据的操作

下一篇:MySQL中怎么处理重复数据

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》