84669 person learning
152542 person learning
20005 person learning
5487 person learning
7821 person learning
359900 person learning
3350 person learning
180660 person learning
48569 person learning
18603 person learning
40936 person learning
1549 person learning
1183 person learning
32909 person learning
When inserting data, can we combine the following requirements into one sql statement?
1, determine whether the data exists;
2, there is no insertion;
3, then query the id field value of the data
走同样的路,发现不同的人生
Probably not possible, you have to do two queries separately, one query and one insert. Unless you call a stored procedure. The stored procedure should be able to achieve your needs
Use insert if not exists
INSERT INTO table(field1, field2, fieldn) SELECT 'field1', 'field2', 'fieldn' FROM DUAL WHERE NOT EXISTS(SELECT field FROM table WHERE field = ?)
MySQL, please use the powerfulREPLACE INTO
MySQL
REPLACE INTO
Mysql can use REPLACE INTO, but it must have a unique index as a condition, but I have not used this, I feel it is more reliable to do it step by step
Probably not possible, you have to do two queries separately, one query and one insert. Unless you call a stored procedure. The stored procedure should be able to achieve your needs
Use insert if not exists
MySQL
, please use the powerfulREPLACE INTO
Mysql can use REPLACE INTO, but it must have a unique index as a condition, but I have not used this, I feel it is more reliable to do it step by step