1

我有一个关系表,它根据它们的 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
4

3 回答 3

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;
于 2012-10-12T19:19:01.840 回答
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(重复,如果不是,则为 null),我使用一个名为 @a 的变量来获取 ID。

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)

它有效,如果您有任何问题,请给我回信

于 2012-10-12T19:22:14.810 回答
1

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

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

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

  1. 复制人到person_temp
  2. 全部删除person
  3. 添加唯一索引到person
  4. 复制unique a,bperson_temp`person.
于 2012-10-12T21:34:29.277 回答