Mysql: If the update data exists, the absence of insert

This article reprinted from: https: //www.cnblogs.com/Eric-zhao/p/6655994.html

mysql syntax supports updates if the data exists, there is no insert, first determine the field data exist or do not exist to set unique index,

Table tb_addrbook example as follows:

 

index: 

 

Statement 1: There is no insert

INSERT INTO tb_addrbook(num,name,mobile) VALUE('1001','小李','13112345678') ON DUPLICATE KEY UPDATE name= '小李',mobile='13112345678'

 

Statement 2: There is updated

INSERT INTO tb_addrbook(num,name,mobile) VALUE('1001','小李','18800000000') ON DUPLICATE KEY UPDATE name= '小李',mobile='18800000000'

 

Guess you like

Origin www.cnblogs.com/alimayun/p/12425662.html