Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete duplicated rows and update the table

Tags:

mysql

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?

like image 806
senzacionale Avatar asked Aug 29 '11 18:08

senzacionale


1 Answers

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.

like image 157
Andomar Avatar answered Sep 22 '22 18:09

Andomar