当列应该变为唯一时合并相关数据

时间:2011-06-14 12:39:24

标签: mysql sql merge one-to-many

给定一个带有表格的mysql数据库如下:

author:
+----+----------+
| id | name     |
+----+----------+
| 1  | John     |
| 2  | Peter    |
| 3  | Peter    |
+----+----------+

article:
+----+-----------+------+
| id | author_id | text |
+----+-----------+------+
| 1  | 2         | ...  |
| 2  | 3         | ...  |
| 3  | 3         | ...  |
+----+-----------+------+

作者表的名称列未被意外设置为唯一。现在我必须将相关文章“合并”到一个相关作者中,即将第2条和第3条的author_id设置为2.我希望之后使名称列唯一。

我无法手动重新分配文章,因为受影响的记录太多。但我认为这个问题可能有一个现成的解决方案/片段。

2 个答案:

答案 0 :(得分:1)

要更新您的article表格,我们可以做到这一点:

update article art
   set art.author_id = (select min(aut.id)
                          from author aut
                         where aut.name = (select a.name
                                             from author a
                                            where a.id = art.author_id));

select * from article;    
+ ------- + -------------- + --------- +
| id      | author_id      | text      |
+ ------- + -------------- + --------- +
| 1       | 2              |           |
| 2       | 2              |           |
| 3       | 2              |           |
+ ------- + -------------- + --------- +
3 rows

如果你更喜欢更紧凑的更新(并且更优化),那么你可以使用这个,它的工作方式相同:

update article art
   set art.author_id = (select min(aut.id)
                          from author aut
                         inner join author a on a.name = aut.name
                         where a.id = art.author_id);

最后,要删除额外作者,您需要

delete a
  from author a
 inner join (
    select name, min(id) as min -- this subquery returns all repeated names and their smallest id
      from author
     group by name
    having count(*) > 1) repeated on repeated.name = a.name
 where a.id > repeated.min;     -- delete all repeateds except the first one

select * from author;    
+ ------- + --------- +
| id      | name      |
+ ------- + --------- +
| 1       | John      |
| 2       | Peter     |
+ ------- + --------- +
2 rows

这适用于任何数量的重复作者。

希望这有帮助

答案 1 :(得分:0)

您可以先执行更新文章,以使用名称相同的最低作者ID

UPDATE art SET art.author_id =
    (SELECT MIN(a1.id) FROM author a1 WHERE a1.Name = a2.name
        FROM article art INNER JOIN author a2 ON art.author_id = a2.id)

然后删除具有相同名称的较高作者

PS。我没有测试过SQL但应该可以工作。