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

时间:2021-07-25 06:24:45

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):表示假如唯一健或者主键已经存在,则更新部分字段.