Given a mysql-database with tables as follows:
author:
+----+----------+
| id | name |
+----+----------+
| 1 | John |
| 2 | Peter |
| 3 | Peter |
+----+----------+
article:
+----+-----------+------+
| id | author_id | text |
+----+-----------+------+
| 1 | 2 | ... |
| 2 | 3 | ... |
| 3 | 3 | ... |
+----+-----------+------+
The author-table’s name-column wasn’t set to unique by accident. Now I have to “merge” related articles into one of the related authors, i.e. set author_id of articles 2 and 3 to 2. I want to make the name-column unique afterwards.
I cannot reassign the articles manually, because there are too many affected records. But I thought there may be a ready solution / snippet for this problem.
To update your
articletable, this will do the trick:if you prefer a more compact update (and more optimized), then you can use this one, that works the same way:
Finally, to delete the extra authors, you need
This works for any number of repeated sets of authors.
Hope this helps