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