DUPLICATE KEY UPDATE 上的字符串比较在 SQL 中不起作用

String comparison ON DUPLICATE KEY UPDATE not working in SQL

INSERT INTO tab1 (head,member, familyName, status, timestamp) 
VALUES ('200519221', '200519239', 'Snowtwo', 'unprocessed', '2020-07-23 18:41:11') 
     ON DUPLICATE KEY UPDATE familyName = 'Snowtwo', status = 'unprocessed', timestamp = IF(STRCMP('unprocessed', status), timestamp, NOW());

当时间戳与未处理状态不匹配时,我正在尝试更新时间戳。它不起作用。

我也试过以下方法

INSERT INTO tab1 (head, member, familyName, status, timestamp) 
VALUES ('200519221', '200519239', 'Snowtwo', 'unprocessed', '2020-07-23 18:41:11') 
     ON DUPLICATE KEY UPDATE familyName = 'Snowtwo', status = 'unprocessed', timestamp = IF(unprocessed = status, timestamp, NOW());

下面的也不行

INSERT INTO tab1 (head, member, familyName, status, timestamp) 
VALUES ('200519221', '200519239', 'Snowtwo', 'unprocessed', '2020-07-23 18:41:11') 
     ON DUPLICATE KEY UPDATE familyName = 'Snowtwo', status = 'unprocessed', timestamp = IF(unprocessed = VALUES(status), timestamp, NOW());

只有当现有状态不是'unprocessed'

时,我才需要更新时间戳
timestamp = IF(unprocessed = VALUES(status), timestamp, NOW());

我错过了什么吗?

I am trying to update the timestamp when it is not matched to unprocessed as status.

我想你想要:

INSERT INTO tab1 (head,member, familyName, status, timestamp) 
VALUES ('200519221', '200519239', 'Snowtwo', 'unprocessed', '2020-07-23 18:41:11') 
ON DUPLICATE KEY UPDATE 
    familyName = VALUES(familyName), 
    status     = VALUES(status), 
    timestamp  = CASE WHEN VALUES(status) = 'unprocessed' 
        THEN timestamp 
        ELSE NOW() 
    END
;

如果新 status 等于 'unprocessed',则 timestamp 保持原样,否则更新为当前日期时间。