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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM