MySQL Insert exists

Source: Internet
Author: User

The scenario is as follows:

"Today's front-end suddenly said, I need to make a judgment, can not repeat the collection, I like the neck of the icy shudder, so think what I should do?" Why did she think that I didn't think it was my job?

Think of three solutions:

1. Two sql, then judged by the first SQL

2. One SQL, use exists in insert (not currently, explained below)

3. Stored Procedures

After consulting the company a colleague, to get the conclusion, if it is two clients operating at the same time using stored procedures, if not, then the first kind of the second is OK, after the decision to adopt the scheme 2

  

INSERT into table (Column1,column2,column3 ... columnn)  SELECT value1,value2,value3 ... valuen from  dual WHERE isn't  EXISTS (        * from        table        =?   );  
SQL Structure

INSERT into content (      detail,      status,      beginTime,      endTime)   SELECT      @detail,       1 ,      NULL,      null from  DUAL      where not EXISTS (          SELECT contentId from           content           WHERE Detail [email protected]);  
SQL Example

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 the existence of this function, it will eliminate the if-else we do cumbersome operation

Finding your own SQL has a long way to go ...

MySQL Insert exists

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.