MySQL in on DUPLICATE KEY update using

Source: Internet
Author: User

Today do inference insert used in MySQL on DUPLICATE KEY UPDATE, now Mark below!

If you want to be able to insert data and update data without data in the database, you can choose on DUPLICATE KEY update.

On DUPLICATE key update can run an update operation on the old row in the presence of a unique index or primary key.

For example: If column A is defined as unique and includes a value of 1, the following two statements have the same effect:

      INSERT into table (a,b,c) VALUES (All-in-all) on DUPLICATE KEY UPDATE C = c + 1,b = b-1;
      UPDATE table SET c = c + 1,b = b-1 WHERE a = 1;


For example, if you insert multiple rows of records (if A is a primary key or A is a unique index column):

      INSERT into TABLE (a,c) VALUES (1,3), (1,7) on DUPLICATE KEY UPDATE C = c + 1;

After running, the value of C becomes 4 (the second is repeated with the first one, and C is +1 on the original value).

      INSERT into TABLE (a,c) values (1,3), (1,7) on DUPLICATE KEY UPDATE C = values (c);

After running, the value of C changes to 7 (the second is repeated with the first one, and C is the value 7 of the direct fetch).


Note:on DUPLICATE KEY update is only a unique syntax for MySQL, not SQL standard syntax!

Official document Photo: Dev.mysql.com/doc/refman/5.1/zh/sql-syntax.html#insert


Related Article

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.