今天在網上找的一個面試題,是擎天科技的面試題最后一題,題目是這樣的:
3.一道SQL題,先找出表中的重復的元素,然后再刪除。
剛開始做這個題剛以為很簡單,就拿一個簡單的例子在我的mysql里試了下手,首先我新建了下面一個數據庫名字為text1,新建表student,插入數據如下圖所示:
+----+------+------+
| id | name | math |
+----+------+------+
|
1 | aaa | 99 |
| 2 | bbb | 98 |
| 3 | aaa | 99 |
| 4 | bbb
| 88 |
| 5 | sss | 88
|
+----+------+------+
有7條數據,只有第一條和第三條是相同的重復數據。
首先
找出重復的元素,select * from student a where a.id!=(select max(id) from student b
where a.name=b.name and a.math=b.math);
可以得出:
+----+------+------+
| id | name | math |
+----+------+------+
|
1 | aaa | 99
|
+----+------+------+
明顯非常正確。(但后來發現如果有數據重復3次或3次以上沒意義了
就必須用如下語句:select name,math,count(*) from student group by name,math having
count(*)>1;)
第二步,刪除重復數據,delete from student a where a.id!=(select max(id) from student b
where a.name=b.name and a.math=b.math);
但是執行的時候,就出錯了:
在一個答案里我看到了一句話:mysql不支持對同一個表查詢后執行修改操作。這樣我就想到了建臨時表。具體步驟如下(數據經過調整):+----+------+------+
|
id | name | math |
+----+------+------+
| 1 | aaa | 99 |
| 2 |
bbb | 98 |
| 3 | aaa | 99 |
| 4 | bbb | 88 |
| 5 | sss |
88 |
| 6 | aaa | 99 |
| 7 | ccc | 99
|
+----+------+------+
mysql> create temporary table s select * from student group by
name,math;
Query OK, 5 rows affected (0.19 sec)
Records: 5 Duplicates: 0
Warnings: 0
mysql> select * from s;(檢查一下是否是需要的數據)
+----+------+------+
| id |
name | math |
+----+------+------+
| 1 | aaa | 99 |
| 4 | bbb |
88 |
| 2 | bbb | 98 |
| 7 | ccc | 99 |
| 5 | sss | 88
|
+----+------+------+
5 rows in set (0.00 sec)
(明顯對了)然后進行表student數據的清除:
mysql> delete from student;
Query OK, 7 rows affected (0.03
sec)
mysql> select * from student;
Empty set (0.00 sec)
然后,再把臨時表的數據寫入表表student中:
mysql> insert into student(id,name,math) select id,name,math from
s;
Query OK, 5 rows affected (0.02 sec)
Records: 5 Duplicates: 0
Warnings: 0
mysql> select * from student;
+----+------+------+
| id | name |
math |
+----+------+------+
| 1 | aaa | 99 |
| 2 | bbb | 98
|
| 4 | bbb | 88 |
| 5 | sss | 88 |
| 7 | ccc | 99
|
+----+------+------+
5 rows in set (0.00 sec)
這樣就實現MYSQL刪除表內重復數據的功能了。