MySQL Update if duplicate row

Nistor Cristian

I have a table with duplicate rows. I want to update the rows before I delete them just to me sure that I delete the good ones.

The delete query is this and works:

DELETE FROM cscart_products 
WHERE updated_timestamp 
NOT IN (SELECT * 
    FROM (SELECT MAX(n.updated_timestamp) 
    FROM cscart_products n 
    GROUP BY n.product_code) x)

Example table:

product_code    product_type    status    updated_timestamp
DUSP1893        P               A         1551
DUSP1893        P               A         322
AH324444        P               A         555
AH324444        P               A         21332

Some products have the same product_code. I want to update those with a smaller updated_timestamp from status A to H.

Thanky you

Aziz Shaikh

First get the rows with smaller timestamp and then update those records, like this:

UPDATE cscart_products
INNER JOIN (
  SELECT product_code pc, MIX(updated_timestamp) ts
  FROM cscart_products
  GROUP BY product_code
) t
ON cscart_products.product_code = t.pc 
AND cscart_products.updated_timestamp = t.ts
SET status = 'H'

Inner query will give you rows of product_code having smaller timestamp.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

mysql query to update duplicate entries

分類Dev

Mysql Update mutiple row of datas

分類Dev

MySQL to update subsequent duplicates of a row

分類Dev

Java+MySQL - adding "on duplicate key update"

分類Dev

Mysql INSERT INTO ... SELECT .... ON DUPLICATE KEY UPDATE

分類Dev

python mysql DUPLICATE KEY UPDATE rrror

分類Dev

How to do update if exist without creating a duplicate row in SQL?

分類Dev

mysql On Duplicate value in field、insert new row with new value

分類Dev

mysql insert on duplicate key update, check which one occurred

分類Dev

Mysql insert on duplicate key update and delete if key not exists

分類Dev

How do I update a row of data in mysql using PHP?

分類Dev

C# - How To Loop Through A Table To Update Each Row MySQL

分類Dev

Cannot delete or update a parent row: a foreign key constraint in grails with mysql

分類Dev

Java to MySQL: How to update row with a value from variable?

分類Dev

PHP delete/update only affects last MySQL row

分類Dev

Update Row with Previous Row

分類Dev

How to On Duplicate Key Update

分類Dev

ON DUPLICATE update nothing

分類Dev

Remove duplicate rows in MySQL

分類Dev

Laravel, Mysql duplicate entry

分類Dev

Delete duplicate rows in mysql

分類Dev

Mysql Duplicate Control and Whitespace

分類Dev

UPDATE, INSERT and ON DUPLICATE KEY UPDATE in one query

分類Dev

HSQLDBの「ON DUPLICATE KEY UPDATE」機能はMYSQLのように動作しませんか?

分類Dev

INSERT INTO ... ON DUPLICATE KEY UPDATEは、PHPとmySQLで重複する値を更新しません

分類Dev

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

分類Dev

MySQL Syntax Error - Trying to update row without deleting/creating a new primary key

分類Dev

Update each row in table

分類Dev

Java JTable update row

Related 関連記事

  1. 1

    mysql query to update duplicate entries

  2. 2

    Mysql Update mutiple row of datas

  3. 3

    MySQL to update subsequent duplicates of a row

  4. 4

    Java+MySQL - adding "on duplicate key update"

  5. 5

    Mysql INSERT INTO ... SELECT .... ON DUPLICATE KEY UPDATE

  6. 6

    python mysql DUPLICATE KEY UPDATE rrror

  7. 7

    How to do update if exist without creating a duplicate row in SQL?

  8. 8

    mysql On Duplicate value in field、insert new row with new value

  9. 9

    mysql insert on duplicate key update, check which one occurred

  10. 10

    Mysql insert on duplicate key update and delete if key not exists

  11. 11

    How do I update a row of data in mysql using PHP?

  12. 12

    C# - How To Loop Through A Table To Update Each Row MySQL

  13. 13

    Cannot delete or update a parent row: a foreign key constraint in grails with mysql

  14. 14

    Java to MySQL: How to update row with a value from variable?

  15. 15

    PHP delete/update only affects last MySQL row

  16. 16

    Update Row with Previous Row

  17. 17

    How to On Duplicate Key Update

  18. 18

    ON DUPLICATE update nothing

  19. 19

    Remove duplicate rows in MySQL

  20. 20

    Laravel, Mysql duplicate entry

  21. 21

    Delete duplicate rows in mysql

  22. 22

    Mysql Duplicate Control and Whitespace

  23. 23

    UPDATE, INSERT and ON DUPLICATE KEY UPDATE in one query

  24. 24

    HSQLDBの「ON DUPLICATE KEY UPDATE」機能はMYSQLのように動作しませんか?

  25. 25

    INSERT INTO ... ON DUPLICATE KEY UPDATEは、PHPとmySQLで重複する値を更新しません

  26. 26

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

  27. 27

    MySQL Syntax Error - Trying to update row without deleting/creating a new primary key

  28. 28

    Update each row in table

  29. 29

    Java JTable update row

ホットタグ

アーカイブ