一个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() 来更新

posted @ 2021-12-22 11:20  小斯林  阅读(1033)  评论(0编辑  收藏  举报