当前位置:Gxlcms > 数据库问题 > mysql存在就更新,不存在就新增

mysql存在就更新,不存在就新增

时间:2021-07-01 10:21:17 帮助过:374人阅读

INSERT INTO newest_log_operation(
    ACTION, OPERATION, KEY_VALUE, BUS_KEY, CONTENT, USER_ID, VERSION, MODULE,CREATE_TIME)
   VALUES (:action, :operation, :keyValue, :busKey, :content, :userId, 1, :module,now())
   ON DUPLICATE KEY UPDATE
    ACTION = VALUES(ACTION),OPERATION = VALUES(OPERATION),BUS_KEY = VALUES(BUS_KEY),CONTENT = VALUES(CONTENT),
    USER_ID = VALUES(USER_ID),VERSION = VERSION + 1,CREATE_TIME = VALUES(CREATE_TIME);

1.ON DUPLICATE KEY UPDATE:表示唯一键重复或者主键重复

2.VALUES (:action, :operation, :keyValue, :busKey, :content, :userId, 1, :module,now()):假如不存在值,则新增记录,并且赋值

3.ACTION = VALUES(ACTION),OPERATION = VALUES(OPERATION),BUS_KEY = VALUES(BUS_KEY),CONTENT = VALUES(CONTENT),
    USER_ID = VALUES(USER_ID),VERSION = VERSION + 1,CREATE_TIME = VALUES(CREATE_TIME):表示假如唯一健或者主键已经存在,则更新部分字段.

mysql存在就更新,不存在就新增

标签:update   value   cti   duplicate   create   action   mod   主键重复   time   

人气教程排行