MySQL - Update rows where value in column is the same as previous row

BadHorsie

I'm trying to clean up some incorrect data:

id   |   name
-------------
1    |   C
2    |   A
3    |   A
4    |   B
5    |   B
6    |   B
7    |   B
8    |   X
9    |   X
10   |   A   
11   |   A      
12   |   A
13   |   X
14   |   X
15   |   B
16   |   C
17   |   C
18   |   X
19   |   A
20   |   A

What has happened is when the data has been entered, if the name field was NULL, the value from the previous iteration of a loop has not been cleared so it has been entered into the next row.

The data should look like this:

id   |   name
-------------
1    |   C
2    |   A
3    |   NULL
4    |   B
5    |   NULL
6    |   NULL
7    |   NULL
8    |   X
9    |   NULL
10   |   A   
11   |   NULL     
12   |   NULL
13   |   X
14   |   NULL
15   |   B
16   |   C
17   |   NULL
18   |   X
19   |   A
20   |   NULL

Is there a way I can update the entire table in one swoop by setting all duplicates like this to NULL, while preserving the values where the column had an intended value?

cn007b

We need to join our duplicates into chain, it easy to reach by:

select *
from updateTable t1
left join updateTable t2 on t1.name = t2.name and t1.id+1 = t2.id
;
+----+------+------+------+
| id | name | id   | name |
+----+------+------+------+
|  1 | C    | NULL | NULL |
|  2 | A    |    3 | A    |
|  3 | A    | NULL | NULL |
|  4 | B    |    5 | B    |
|  5 | B    |    6 | B    |
|  6 | B    |    7 | B    |
|  7 | B    | NULL | NULL |
|  8 | X    |    9 | X    |
|  9 | X    | NULL | NULL |
| 10 | A    |   11 | A    |
| 11 | A    |   12 | A    |
| 12 | A    | NULL | NULL |
| 13 | X    |   14 | X    |
| 14 | X    | NULL | NULL |
| 15 | B    | NULL | NULL |
| 16 | C    |   17 | C    |
| 17 | C    | NULL | NULL |
| 18 | X    | NULL | NULL |
| 19 | A    |   20 | A    |
| 20 | A    | NULL | NULL |
+----+------+------+------+

Now we know our ids that should be updated.
But we cannot run:

update updateTable set name = null where id in (
    select t2.id
    from updateTable t1
    left join updateTable t2 on t1.name = t2.name and t1.id+1 = t2.id
    where t2.id is not null
);

because we will receive error:

ERROR 1093 (HY000): You can't specify target table 'updateTable' for update in FROM clause

but we can avoid this bug by using temporary table for ids:

create temporary table updateTableTmp (
    id int,
    primary key (id)
) engine=innodb;

insert into updateTableTmp
select t2.id
from updateTable t1
left join updateTable t2 on t1.name = t2.name and t1.id+1 = t2.id
where t2.id is not null
;

update updateTable set name = null where id in (
    select id from updateTableTmp
);

select * from updateTable;
+----+------+
| id | name |
+----+------+
|  1 | C    |
|  2 | A    |
|  3 | NULL |
|  4 | B    |
|  5 | NULL |
|  6 | NULL |
|  7 | NULL |
|  8 | X    |
|  9 | NULL |
| 10 | A    |
| 11 | NULL |
| 12 | NULL |
| 13 | X    |
| 14 | NULL |
| 15 | B    |
| 16 | C    |
| 17 | NULL |
| 18 | X    |
| 19 | A    |
| 20 | NULL |
+----+------+

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

MySQL update column based on previous row (same column)

From Dev

Select specific rows based on previous row value (in the same column)

From Dev

MySQL - Update table and set column value equal to different of current row value minus previous row value

From Dev

mysql query select multiple rows where column value is same

From Dev

Mysql update all rows value with count of same table column

From Dev

MYSQL multiply column value by value in previous row

From Dev

Selecting rows where column value changed from previous row, user variables, innodb

From Dev

Pandas DataFrame - delete rows that have same value at a particular column as a previous row

From Dev

Count rows where value in row is also in previous row

From Dev

Sum ONLY at the last row where previous rows have same values

From Dev

MYSQL update a row if all multiple rows that belong to the same foreign ID have same value

From Dev

SQL rows sharing same column value and with where

From Dev

MySQL count rows where column value is same and select them where count is greater than 2

From Dev

mysql how to update a column of all rows and for each row a different column value

From Dev

DAX for populating column value based on previous row of same column

From Dev

Creating a mySQL view where each row is a specific column's value in the same row

From Dev

Remove rows from Dataframe where row above or below has same value in a specific column

From Dev

In SQL how to not select rows where a column value is the same as the one in the row above it

From Dev

Update a row if a column value is not the same on other table

From Java

Calculate value based on value from same column of the previous row in spark

From Dev

python - use previous row's value to update the new rows values

From Dev

delete rows based in the value of the previous row in the same table

From Dev

Update Column value for all rows in Table where Column Value Is Null?

From Dev

MySQL Select the rows having same column value

From Dev

MySQL - select row by user_id based on column value of other rows with the same user_id

From Dev

MySql get all rows where id = xxx and where the newest column in other table in row with same id is greater than one

From Java

Mysql select row with same value column

From Dev

MySQL getting value in same column as where conditional

From Dev

Update MySQL datetime column with subtracted value from another row in same table

Related Related

  1. 1

    MySQL update column based on previous row (same column)

  2. 2

    Select specific rows based on previous row value (in the same column)

  3. 3

    MySQL - Update table and set column value equal to different of current row value minus previous row value

  4. 4

    mysql query select multiple rows where column value is same

  5. 5

    Mysql update all rows value with count of same table column

  6. 6

    MYSQL multiply column value by value in previous row

  7. 7

    Selecting rows where column value changed from previous row, user variables, innodb

  8. 8

    Pandas DataFrame - delete rows that have same value at a particular column as a previous row

  9. 9

    Count rows where value in row is also in previous row

  10. 10

    Sum ONLY at the last row where previous rows have same values

  11. 11

    MYSQL update a row if all multiple rows that belong to the same foreign ID have same value

  12. 12

    SQL rows sharing same column value and with where

  13. 13

    MySQL count rows where column value is same and select them where count is greater than 2

  14. 14

    mysql how to update a column of all rows and for each row a different column value

  15. 15

    DAX for populating column value based on previous row of same column

  16. 16

    Creating a mySQL view where each row is a specific column's value in the same row

  17. 17

    Remove rows from Dataframe where row above or below has same value in a specific column

  18. 18

    In SQL how to not select rows where a column value is the same as the one in the row above it

  19. 19

    Update a row if a column value is not the same on other table

  20. 20

    Calculate value based on value from same column of the previous row in spark

  21. 21

    python - use previous row's value to update the new rows values

  22. 22

    delete rows based in the value of the previous row in the same table

  23. 23

    Update Column value for all rows in Table where Column Value Is Null?

  24. 24

    MySQL Select the rows having same column value

  25. 25

    MySQL - select row by user_id based on column value of other rows with the same user_id

  26. 26

    MySql get all rows where id = xxx and where the newest column in other table in row with same id is greater than one

  27. 27

    Mysql select row with same value column

  28. 28

    MySQL getting value in same column as where conditional

  29. 29

    Update MySQL datetime column with subtracted value from another row in same table

HotTag

Archive