如果设置为 1,则避免更新列

Avoid updating column if set to 1

我有这个查询:

INSERT INTO user_list (USER_ID,USERNAME,NAME,ACTIVITY,PRIVATE)
VALUES(?,?,?,1,?) 
ON DUPLICATE KEY UPDATE USERNAME=?, NAME=?, ACTIVITY=ACTIVITY+1,PRIVATE=?

PRIVATE可以是0也可以是1,我需要根据一个参数插入,但是如果PRIVATE在那一行已经是1,就不应该设置成0了。

基本上如果它变成1,它永远不会变成0

如何在单个查询中执行此操作?

嗯。 . .为什么要更改值?

INSERT INTO user_list (USER_ID, USERNAME, NAME, ACTIVITY, PRIVATE) 
    VALUES(?, ?, ?, 1, ?)
    ON DUPLICATE KEY UPDATE USERNAME=?, NAME=?, ACTIVITY=ACTIVITY+1;

还有,你知道VALUES()吗?

INSERT INTO user_list (USER_ID, USERNAME, NAME, ACTIVITY, PRIVATE) 
    VALUES(?, ?, ?, 1, ?)
    ON DUPLICATE KEY UPDATE USERNAME = VALUES(USERNAME),
                            NAME = VALUES(NAME),
                            ACTIVITY = ACTIVITY + 1;

这会将传入的值分配给这些列。

用例确定 private 是否应保持原样,如果是则用 self 覆盖。

insert into user_list(id,name,privacy)
values
(1,'aaa','0')
on duplicate key 
update name = values(name)
        ,
         privacy = case when privacy = 0 then values(privacy)
                              when privacy = 1 then privacy
                        end
;

可以设置列的值PRIVATE:

PRIVATE = PRIVATE OR VALUES(PRIVATE)

如果这是原始值,它将保留值 1,如果原始值为 0:

,则将其更改为新值
INSERT INTO user_list (USER_ID, USERNAME, NAME, ACTIVITY, PRIVATE)
VALUES(?, ?, ?, 1, ?) 
ON DUPLICATE KEY UPDATE USERNAME = VALUES(USERNAME), 
                        NAME = VALUES(NAME), 
                        ACTIVITY = ACTIVITY + 1, 
                        PRIVATE = PRIVATE OR VALUES(PRIVATE)