MySQL statement level avoids repeated insertions--insert Select not Exist

Source: Internet
Author: User

Want to insert a piece of data, to avoid repeated insertions, and do not want to toss two back to the database connection operation, you can refer to the following methods.

INSERT  into Table (Column1,column2,column3 ... columnn) SELECT value1,value2,value3 ... valuen  from Dual WHERE  not EXISTS (      SELECT*from      table      WHERE =  ?);


dual is a table that exists to build query statements that are common in Oracle, with the INSERT ... SELECT builds the table that we need and specifies the data item.
EXISTS Through this judgment whether the existence of the function, it is exempt from our if-else to do the cumbersome operation.

Cases:

            INSERT  intocontent (detail, status, BeginTime, EndTime) SELECT                @detail,                1,                NULL,                NULL             fromDUALWHERE  not EXISTS(                 SELECTcontentId fromcontentWHEREDetail=@detail);


@detail is the content to be deposited, the content is retrieved, if you want to do so, it is best to do a unique constraint on the field, or indexed.
Dispense with the If-else, in the Ibatis configuration is OK, ha!

There is a more determined approach--replace into:

Replace  into' taisau '. ' RecordInfo ' (' WorkID ', ' bartificialattendance ', ' fthreshold ', ' attendance_time ', ' Picpath ') Select '%s','1','60.00','%d-%02d-%02d%02d:%02d:%02d.000','%s'   fromDualwhere  not exists(SELECT' Attendance_time ' from' Taisau '. ' RecordInfo 'WHERE' WorkID '= '%s'   and' Attendance_time '= '%d-%02d-%02d%02d:%02d:%02d.000');

MySQL statement level avoids repeated insertions--insert Select not Exist

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.