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> 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)
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/15498/viewspace-2124027/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- mysql表刪除重複記錄方法MySql
- MYSQL中刪除重複記錄的方法薦MySql
- 用SQL語句刪除重複記錄的四種方法SQL
- Oracle如何刪除表中重複記錄Oracle
- 查詢刪除表中重複記錄
- 刪除重複id的記錄
- 刪除Oracle重複記錄Oracle
- oracle刪除重複記錄Oracle
- Oracle 刪除表中重複記錄的DELETE SQLOracledeleteSQL
- 高效快速刪除Oracle表中重複記錄Oracle
- Oracle查詢重複資料與刪除重複記錄方法Oracle
- MySql避免重複插入記錄的幾種方法MySql
- 處理表重複記錄(查詢和刪除)
- MySQL刪除重複記錄並保留第一條MySql
- MySQL資料庫中刪除重複記錄的方法總結[推薦]MySql資料庫
- mysql 刪除表中重複的資料MySql
- 三種方法刪除列表中重複的元素及效率分析!
- mysql查詢表裡的重複資料方法和刪除重複資料MySql
- 【探索】兩種查詢和刪除重複記錄的方法及其效能比較
- db2刪除重複的記錄DB2
- oracle-快速刪除重複的記錄Oracle
- mysql刪除重複記錄,儲存Id最小的一條MySql
- DB2 刪除重複記錄DB2
- oracle 刪除重複資料的幾種方法Oracle
- Oracle如何刪除表中重複記錄保留第一條Oracle
- 經典SQL面試題4:高效的刪除重複記錄方法SQL面試題
- oracle 快速刪除大批量資料方法(全部刪除,條件刪除,刪除大量重複記錄)Oracle
- Oracle查詢重複資料與刪除重複記錄Oracle
- Oracle使用over()partition by刪除重複記錄Oracle
- mysql 刪除重複項MySql
- 刪除重複資料的一種高效的方法
- 刪除重複資料的三種sql寫法SQL
- Mysql語句查詢指定重複記錄和刪除重複記錄僅保留一條【親測可以】MySql
- oracle 快速刪除大批量資料方法(全部刪除,條件刪除,刪除大量重複記錄) 轉Oracle
- 【轉】oracle 快速刪除大批量資料方法(全部刪除,條件刪除,刪除大量重複記錄)Oracle
- Java刪除ArrayList中的重複元素的2種方法Java
- sql刪除重複記錄只保留一條SQL
- MySQL刪除重複資料MySql