mysql 4.0 删除重复_mysql删除重复数据
最近遇到刪除重復(fù)數(shù)據(jù)的問題,先分享一下解決辦法,如有不完善之處還望包涵!
舉例如下:
mysql> select * from table03;
+----+------+--------+
| id | name | degree |
+----+------+--------+
| 1 | fly | 90 |
| 2 | fly | 90 |
| 3 | fly | 90 |
| 4 | fly | 80 |
| 5 | wang | 90 |
| 6 | wang | 90 |
| 7 | wang | 90 |
| 8 | wang | 80 |
| 9 | yang | 90 |
| 10 | yang | 90 |
| 11 | yang | 90 |
| 12 | yang | 80 |
+----+------+--------+
12 rows in set (0.00 sec)
表中三個(gè)字段,現(xiàn)要?jiǎng)h除除了id相同其他都相同的記錄。
也許有人會(huì)使用;
mysql> delete from table03where id not in(select min(id) from table03 group by name, degree);
這是你會(huì)發(fā)現(xiàn)Mysql會(huì)報(bào)錯(cuò):
ERROR 1093 (HY000): You can'tspecify target table 'table03' for update in FROM clause
不能在一條語句中先select一個(gè)表中的某些值再update這個(gè)表
但是下面的語句可以正常執(zhí)行
mysql> select * from table03where id not in(select min(id) from table03 group by name, degree);
+----+------+--------+
| id | name | degree |
+----+------+--------+
| 2 | fly | 90 |
| 3 | fly | 90 |
| 6 | wang | 90 |
| 7 | wang | 90 |
| 10 | yang | 90 |
| 11 | yang | 90 |
+----+------+--------+
6 rows in set (0.07 sec)
我們會(huì)發(fā)現(xiàn)這些數(shù)據(jù)就是我們要?jiǎng)h除的,因此,可以先把分組信息做成虛表,然后從虛表中選出結(jié)果,最后再將結(jié)果作為刪除的條件數(shù)據(jù)
mysql> delete from table03where id not in(select min(id) from (
-> select * from table03) as t group byt.name ,t.degree);
Query OK, 6 rows affected (0.06sec)
mysql> select * from table03;
+----+------+--------+
| id | name | degree |
+----+------+--------+
| 1 | fly | 90 |
| 4 | fly | 80 |
| 5 | wang | 90 |
| 8 | wang | 80 |
| 9 | yang | 90 |
| 12 | yang | 80 |
+----+------+--------+
6 rows in set (0.00 sec)
問題完美解決。
《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
以上是生活随笔為你收集整理的mysql 4.0 删除重复_mysql删除重复数据的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: dz mysql日志清理_Discuz教
- 下一篇: mysql 值到99999后不增值了_M