SQL Insert multiple record while using ON DUPLICATE KEY UPDATE

DayIsGreen

I have a query that inserts multiple data in one go as a single query.

INSERT INTO tableName (COLUMN_1, COLUMN_2, COLUMN_3) 
SELECT 'test1', 'test2', 'test3'
UNION ALL
SELECT 'test4', 'test5', 'test6'
UNION ALL
SELECT 'test7', 'test8', 'test8'

Using the above code, is there a way to implement the 'ON DUPLICATE KEY UPDATE'? Something like:

INSERT INTO tableName (COLUMN_1, COLUMN_2, COLUMN_3)
SELECT 'test1', 'test2', 'test3'
ON DUPLICATE KEY UPDATE COLUMN_1='new', COLUMN_2='new', COLUMN_3='new'
UNION ALL
SELECT 'test1', 'test2', 'test3'
ON DUPLICATE KEY UPDATE COLUMN_1='new', COLUMN_2='new', COLUMN_3='new'
UNION ALL
SELECT 'test1', 'test2', 'test3'
ON DUPLICATE KEY UPDATE COLUMN_1='new', COLUMN_2='new', COLUMN_3='new';

Note: May be irrelevant or pointless, but I'm doing this with mysql workbench.

Nick

Your query is basically correct, just get rid of the intermediate ON DUPLICATE KEY.... There is no need for a derived table because you are not referencing columns from the union.

INSERT INTO tableName (COLUMN_1, COLUMN_2, COLUMN_3) 
SELECT 'test8', 'test9', 'test10'
UNION ALL
SELECT 'test4', 'test5', 'test6'
UNION ALL
SELECT 'test9', 'test5', 'test6'
ON DUPLICATE KEY UPDATE COLUMN_1='new', COLUMN_2='new', COLUMN_3='new';

The problem you are going to run into is if you get more than one duplicate key on the INSERT. In that case the UPDATE will attempt to set two rows to have the same key ('new') and the INSERT will fail. You could potentially work around this by changing the query so that the UPDATE includes part of the old column value. In this case since you'll be referring to a column value you will need a derived table:

INSERT INTO tableName (COLUMN_1, COLUMN_2, COLUMN_3) 
SELECT * FROM (
    SELECT 'test8', 'test9', 'test10'
    UNION ALL
    SELECT 'test4', 'test5', 'test6'
    UNION ALL
    SELECT 'test1', 'test5', 'test6') AS dt
ON DUPLICATE KEY UPDATE COLUMN_1=CONCAT('new', COLUMN_1), COLUMN_2='new', COLUMN_3='new';

Updated SQLFiddle

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

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

編集
0

コメントを追加

0

関連記事

分類Dev

UPDATE, INSERT and ON DUPLICATE KEY UPDATE in one query

分類Dev

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

分類Dev

PostgreSQL duplicate key value violates unique constraint while using on conflict do nothing on insert

分類Dev

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

分類Dev

Go:ON DUPLICATE KEY UPDATEでSQL RowsAffected

分類Dev

When trying to update multiple records Rails can't find the record with id while using friendly_id

分類Dev

mysql insert on duplicate key update, check which one occurred

分類Dev

How to On Duplicate Key Update

分類Dev

PHP Foreach SQL、INSERT INTO ON DUPLICATE KEY UPDATEは最後のクエリのみを挿入しますか?

分類Dev

PostgreSQL BIGSERIAL and "duplicate key" on insert

分類Dev

How to update table using foreign key in sql

分類Dev

Nested WHILE Loop with INSERT produced duplicate records

分類Dev

Insert or update data using cte_results in SQL Server

分類Dev

Update Temp table to Insert node in XML data using SQL

分類Dev

INSERT INTO WHERE NOT EXISTS - Getting Duplicate Key Error

分類Dev

cannot insert duplicate key in object exception ef

分類Dev

MySQLは、INSERT INTO SELECT ... ON DUPLICATE KEY UPDATE ... SQLステートメントを使用すると、更新する代わりに新しい行を作成します。

分類Dev

VB.NETで「INSERT INTO table VALUES() ON DUPLICATE KEY UPDATE」を使用できません

分類Dev

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

分類Dev

SQL interview- how to get the record if there is no record or count is zero using multiple tables

分類Dev

Java+MySQL - adding "on duplicate key update"

分類Dev

python mysql DUPLICATE KEY UPDATE rrror

分類Dev

Insert and update a datetime into SQL database

分類Dev

Can't update then insert in SQL

分類Dev

SQL Script to Insert New Record Per Id

分類Dev

Pyspark multiple records of same key into single record

分類Dev

Can we access a record in SQL table using primary key in O(1) time?

分類Dev

insert record in android studio (kotlin) using postgresql

分類Dev

Bash Using variable names in sed to insert record

Related 関連記事

  1. 1

    UPDATE, INSERT and ON DUPLICATE KEY UPDATE in one query

  2. 2

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

  3. 3

    PostgreSQL duplicate key value violates unique constraint while using on conflict do nothing on insert

  4. 4

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

  5. 5

    Go:ON DUPLICATE KEY UPDATEでSQL RowsAffected

  6. 6

    When trying to update multiple records Rails can't find the record with id while using friendly_id

  7. 7

    mysql insert on duplicate key update, check which one occurred

  8. 8

    How to On Duplicate Key Update

  9. 9

    PHP Foreach SQL、INSERT INTO ON DUPLICATE KEY UPDATEは最後のクエリのみを挿入しますか?

  10. 10

    PostgreSQL BIGSERIAL and "duplicate key" on insert

  11. 11

    How to update table using foreign key in sql

  12. 12

    Nested WHILE Loop with INSERT produced duplicate records

  13. 13

    Insert or update data using cte_results in SQL Server

  14. 14

    Update Temp table to Insert node in XML data using SQL

  15. 15

    INSERT INTO WHERE NOT EXISTS - Getting Duplicate Key Error

  16. 16

    cannot insert duplicate key in object exception ef

  17. 17

    MySQLは、INSERT INTO SELECT ... ON DUPLICATE KEY UPDATE ... SQLステートメントを使用すると、更新する代わりに新しい行を作成します。

  18. 18

    VB.NETで「INSERT INTO table VALUES() ON DUPLICATE KEY UPDATE」を使用できません

  19. 19

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

  20. 20

    SQL interview- how to get the record if there is no record or count is zero using multiple tables

  21. 21

    Java+MySQL - adding "on duplicate key update"

  22. 22

    python mysql DUPLICATE KEY UPDATE rrror

  23. 23

    Insert and update a datetime into SQL database

  24. 24

    Can't update then insert in SQL

  25. 25

    SQL Script to Insert New Record Per Id

  26. 26

    Pyspark multiple records of same key into single record

  27. 27

    Can we access a record in SQL table using primary key in O(1) time?

  28. 28

    insert record in android studio (kotlin) using postgresql

  29. 29

    Bash Using variable names in sed to insert record

ホットタグ

アーカイブ