数据库
首页 > 数据库> > 一个sql语句实现数据的有则更新,无则插入

一个sql语句实现数据的有则更新,无则插入

作者:互联网

sql格式:

insert into 表名(列名,列名)  values(值,值) on duplicate key update 列名=值;

 

例子(mybatis里):

insert into usee(uuid,name,count,last_update_time)  values(#{uuid},#{name},1,now()) on duplicate key update count=count+1,last_update_time=now();

这句sql的前提是需要创建uuid的唯一索引,意思就是数据库表如果没有相同的uuid就会执行 insert into usee(uuid,name,count,last_update_time)  values(#{uuid},#{name},1,now())  来新增

如果有相同的uuid,就会执行 count=count+1,last_update_time=now() 来更新

标签:语句,count,now,last,uuid,update,sql,无则,name
来源: https://www.cnblogs.com/feiyuexiong/p/15718698.html