MySQL: ONDUPLICATEKEYUPDATE usage _ MySQL

Source: Internet
Author: User
MySQL: ONDUPLICATEKEYUPDATE

This statement is in mysql, but not in standard SQL statements.

Insert into .. on duplicate key to update multiple rows

If on duplicate key update is specified at the end of the INSERT statement, and DUPLICATE values appear in a UNIQUE index or primary key after the row is inserted, the old row UPDATE is executed; if the unique value column is not duplicate, a new row is inserted. For example, if Column a is defined as UNIQUE and contains a value of 1, the following two statements have the same effect:
Originally, three SQL statements were required:
The code is as follows:
IF (SELECT * FROM t_table WHERE id = 1001 ') {UPDATE t_table SET cnt = cnt + 1 WHERE id = 1001;} else {insert into ipstats (id, cnt) VALUES (1001, 1 );}
The code is as follows:
Insert into t_table (id, cnt) values (1001,2) on duplicate key update cnt = cnt + 1;
Update t_table set cnt = cnt + 1 WHERE id = 1001;

If a row is inserted as a new record, the value of the affected row is 1. if the original record is updated, the value of the affected row is 2.

Now the question is, how can I specify the value of the field after ON DUPLICATE KEY UPDATE if multiple rows are inserted? You must know that only one on duplicate key update exists in an INSERT statement. it will UPDATE a row of records or all rows to be updated. This problem has plagued me for a long time. In fact, all problems are solved by using the VALUES () function.

For example, field a is defined as UNIQUE, and records (, 9) and (, 1) exist in the table of the original database ), if the value of the inserted record is the same as the original record, the original record is updated; otherwise, a new row is inserted:

The code is as follows:
Insert into table (a, B, c) VALUES (, 3), (, 7), (, 6), (, 2) on duplicate key update B = VALUES (B );

When the preceding SQL statement is executed, if a in (, 7) conflicts with the unique value of the original record (, 9), the ON DUPLICATE KEY UPDATE statement is executed to UPDATE the original record, 9) update (, 9), update (, 1) to (, 3, 1), insert new Records (, 3), and (, 2)

Note: on duplicate key update is only a special MySQL syntax, not a standard SQL syntax!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.