Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6029965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:57:58+00:00 2026-05-23T04:57:58+00:00

Given a mysql-database with tables as follows: author: +—-+———-+ | id | name |

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T04:57:58+00:00Added an answer on May 23, 2026 at 4:57 am

    To update your article table, this will do the trick:

    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
    

    if you prefer a more compact update (and more optimized), then you can use this one, that works the same way:

    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);
    

    Finally, to delete the extra authors, you need

    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
    

    This works for any number of repeated sets of authors.

    Hope this helps

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a table named person (in a MySQL database/schema), kind of like this one:
I have 3 tables in my SQL database as follows Users2 UserID, EID, Name,
I was given a MySQL database file that I need to restore as a
From a stored procedure or function in a given MySQL database, is it possible
I have a many to many table setup in my mysql database. Teams can
I have a mysql database with movies as follows: MOVIES(id,title) KEYWORDS_TABLE(id,key_id) [id is referenced
How do I view the grants (access rights) for a given user in MySQL?
I have a MySQL table holding lots of records that i want to give
I need to query the database by joining two tables. Here is what I
For a specific database in a MySQL Server 5.5 I would like to view

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.