這就是你如何刪除重複的行...我會寫你我的例子,你需要適用於你的代碼。我有ID
演員表,我想刪除行重複first_name
mysql> select actor_id, first_name from actor_2;
+----------+-------------+
| actor_id | first_name |
+----------+-------------+
| 1 | PENELOPE |
| 2 | NICK |
| 3 | ED |
....
| 199 | JULIA |
| 200 | THORA |
+----------+-------------+
200 rows in set (0.00 sec)
- 現在我使用了一個名爲@a變量獲得ID下一行是否具有相同的FIRST_NAME(重複,無效,如果它是不)。
mysql> select if([email protected],actor_id,null) as first_names,@a:=first_name from actor_2 order by first_name;
+---------------+----------------+
| first_names | @a:=first_name |
+---------------+----------------+
| NULL | ADAM |
| 71 | ADAM |
| NULL | AL |
| NULL | ALAN |
| NULL | ALBERT |
| 125 | ALBERT |
| NULL | ALEC |
| NULL | ANGELA |
| 144 | ANGELA |
...
| NULL | WILL |
| NULL | WILLIAM |
| NULL | WOODY |
| 28 | WOODY |
| NULL | ZERO |
+---------------+----------------+
200 rows in set (0.00 sec)
- 現在我們只能得到複製ID:
mysql> select first_names from (select if([email protected],actor_id,null) as first_names,@a:=first_name from actor_2 order by first_name) as t1;
+-------------+
| first_names |
+-------------+
| NULL |
| 71 |
| NULL |
...
| 28 |
| NULL |
+-------------+
200 rows in set (0.00 sec)
-the最後一步,讓我們刪除!
mysql> delete from actor_2 where actor_id in (select first_names from (select if([email protected],actor_id,null) as first_names,@a:=first_name from actor_2 order by first_name) as t1);
Query OK, 72 rows affected (0.01 sec)
- 現在讓我們檢查表:
mysql> select count(*) from actor_2 group by first_name;
+----------+
| count(*) |
+----------+
| 1 |
| 1 |
| 1 |
...
| 1 |
+----------+
128 rows in set (0.00 sec)
它的工作原理,如果你有任何問題,我寫回
可能接受的答案在這個問題上也將對你有所幫助:http://stackoverflow.com/questions/18932/how-can-i-remove-duplicate-rows – MBozic
你有任何ID或什麼獨特的?你可以展示桌子的結構嗎? – jcho360