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 6381021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:20:34+00:00 2026-05-25T02:20:34+00:00

I have duplicated rows in tables. I have two table which are connected by

  • 0

I have duplicated rows in tables.

I have two table which are connected by a foreign key

regions (id)
orders (region_id)

The regions have duplicated names. I want to delete these duplicated rows and update orders table that duplicated foreign key will now be set to only left existing name in regions table.

Example:

regions table:

id  name
1 | test
2 | test
3 | foo

orders table:

id region_id
6 | 1
7 | 2
9 | 3

I want

orders table:

id region_id
6 | 1
7 | 1
9 | 3

regions table:

id  name
1 | test
3 | foo

I can get duplicated rows with this SQL:

SELECT name, count(id) as cnt FROM regions 
GROUP BY name HAVING cnt > 1

How can I connect this select with order table and delete duplicated rows and update the table?

  • 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-25T02:20:34+00:00Added an answer on May 25, 2026 at 2:20 am

    To update the orders table, something like:

    update  orders
    join    regions r1
    on      r1.id = orders.region_id
    set     orders.region_id = 
            (
            select  min(r2.id)
            from    regions r2
            where   r2.name = r1.name
            )
    

    After that, you can delete duplicate rows with:

    delete  regions
    from    regions
    where   id not in
            (
            select  id
            from    (
                    select  min(id) as id
                    from    regions
                    group by
                        name
                    ) as SubqueryAlias
            )
    

    The double subquery is required to avoid the MySQL error ERROR 1093 (HY000) at line 36: You can't specify target table 'regions' for update in FROM clause.

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

Sidebar

Related Questions

I have a table and I want to duplicate specific rows in the table.
I have two columns from a table; fromid and toid , which contain the
I have a table with some duplicate rows that I want to normalize into
i have two tables which form part of my login/register system: these are userlogin
I have two tables, A and B, and I am trying to select rows
I have two tables structured like this: table a id title date 1 testing1
In an existing application, I have a table which has no primary key, which
I have the following tables: Category table which has an ID column, a description
I have a table in which i append rows on onclick event, the row
I have two dependent tables as CREATE TABLE posts ( post_id int(11) unsigned NOT

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.