本篇文章給大家分享的是有關MySQL中如何刪除表重復記錄,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
10多年的敘州網(wǎng)站建設經(jīng)驗,針對設計、前端、開發(fā)、售后、文案、推廣等六對一服務,響應快,48小時及時工作處理。網(wǎng)絡營銷推廣的優(yōu)勢是能夠根據(jù)用戶設備顯示端的尺寸不同,自動調(diào)整敘州建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設計,從而大程度地提升瀏覽體驗。創(chuàng)新互聯(lián)從事“敘州網(wǎng)站設計”,“敘州網(wǎng)站推廣”以來,每個客戶項目都認真落實執(zhí)行。
創(chuàng)建實驗表student表數(shù)據(jù):
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中如何刪除表重復記錄,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學到更多知識。更多詳情敬請關注創(chuàng)新互聯(lián)行業(yè)資訊頻道。