MYSQL中replace into的用法

replace into是insert into的增强版,

在向表中插入数据时,我们经常会遇到这样的情况:

  1、首先判断数据是否存在;2、如果不存在,则插入;3、如果存在,则更新。

replace into table(id, update_time) values(1, now());
replace into table(id, update_time) select 1, now();
1. replace into table(col, ...) values(...)
2. replace into table(col, ...) select ...
3. replace into table set col=value, ...

  

SQL SERVER中可以这样写

if not exists (select 1 from table where id = 1) insert into table(id, update_time) values(1, getdate()) else update table set update_time = getdate() where id = 1

  

原文地址:https://www.cnblogs.com/zhoucx66/p/5715447.html