删除重复行,没有唯一键 - 关系表

时间:2012-10-12 19:14:29

标签: mysql sql

我有一个关系表,根据其ID连接另外两个表。两列都可以有重复 - 但不能同一行两次。我处理检查代码方面。

如何删除重复的行(见下文):

select * from people:

a | b
1   2
1   3
1   3
1   7
2   3
2   5
2   5
2   9

我希望结果是:

a | b
1   2
1   3
1   7
2   3
2   5
2   9

3 个答案:

答案 0 :(得分:3)

这应该有效:

ALTER IGNORE TABLE people ADD UNIQUE (a,b);

如果您不想添加索引,那么这应该有效:

DROP TABLE IF EXISTS people_old;
DROP TABLE IF EXISTS people_new;
CREATE TABLE people_new LIKE people;
INSERT INTO people_new SELECT DISTINCT * FROM people;
RENAME TABLE people TO people_old, people_new TO people;

答案 1 :(得分:1)

这是您删除重复行的方法...我将把您的示例写给您,您需要申请代码。我有带ID的Actors表,我想删除重复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)

- 如果下一行具有相同的first_name,则使用名为@a的变量获取ID(重复,如果不是,则返回null)。

mysql> select if(first_name=@a,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(first_name=@a,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)

- 最后一步,让我们删除!

mysql> delete from actor_2 where actor_id in (select first_names from (select if(first_name=@a,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)

它有效,如果您有任何问题请写回来

答案 2 :(得分:1)

  

两列都可能有重复 - 但两行不能相同

这是对您尚未实现的表的约束。约束是unique index上的(a,b)。如果您有索引,则不会有重复项。

恕我直言,你最好的方法是向表中添加唯一索引,使用临时表首先删除重复项:

  1. 将人员复制到person_temp
  2. person
  3. 删除所有内容
  4. person
  5. 添加唯一索引
  6. unique a,bperson_temp复制到'person。