mysql不存在插入否则更新

1.如果存在就更新,没有就插入,相当于三目运算

CREATE UNIQUE INDEX xxx ON t_msg_user_view(USER_ID);

INSERT INTO xxx(user_id,create_time,update_time,STATUS) VALUE('1001',NOW(),NOW(),1)
ON DUPLICATE KEY UPDATE update_time= NOW(),STATUS=1

2.如果不满足就执行insert

INSERT INTO xxx(USER_ID, USER_NAME, VIEW_LEVEL, CREATE_TIME, OPERATOR, STATUS)
SELECT :userId, :userName, :viewLevel, now(), :operator, 1
WHERE NOT EXISTS(SELECT USER_ID FROM xxxWHERE USER_ID=:userId AND STATUS = 1)

posted on 2018-12-28 15:37  cxhfuujust  阅读(267)  评论(0编辑  收藏  举报

导航