MySQL insert update on a SQL on DUPLICATE KEY update

Source: Internet
Author: User
Tags mysql insert

Sometimes data operations are required, and data is updated if there is data, and no data is inserted. The previous practice is to query, and then based on the query results to judge, to perform the INSERT or update operation

There is actually an on DUPLICATE KEY UPDATE syntax that allows you to do this in one SQL.

INSERT into Tbprize (Sopenid,iareaid,iplatid,sssgroupone,sssgrouptwo,ssgroupone,ssgrouptwo,sgroupone,sgrouptwo, Dtfound) VALUES (' 2a4528699714bc8016daed06e245163f ', 1038,1038,556175,556172,111,222,556144,556137, ' 2018-03-20 11:28:30 ')
On DUPLICATE KEY UPDATE sopenid = ' 2a4528699714bc8016daed06e245163f ', ssgroupone=777,ssgrouptwo=888

  1. Added after normal INSERT statement, on DUPLICATE KEY UPDATE Field name = value, multiple values comma separated
  2. Must have a primary key or a unique key, the optional statement does not take effect

MySQL insert update on a SQL on DUPLICATE KEY update

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.