我在表格中遇到更新问题。在我的下方,我有更新userdeleted
列的标记,我还想更改CurrentTimestamp
值的那一行的值,但在SET
中再添加一个条件后,我的所有行值更新为当前时间戳。
UPDATE dpuserapplication t1
LEFT OUTER JOIN temp_userapplication t2
ON t1.userName = t2.userName
AND t1.ApplicationName = t2.ApplicationName
AND t1.groupName = t2.groupName
SET t1.userDeleted = CASE WHEN t2.ID IS NULL THEN 1 ELSE 0 END,
t1.CreatedDate = CURRENT_TIMESTAMP;
答案 0 :(得分:0)
这是因为LEFT OUTER JOIN
会返回dpuserapplication
的所有行,除非您指定WHERE
子句。由于您没有指定任何修改时间戳的条件,因此会在每一行上修改它。
尝试添加条件:
UPDATE dpuserapplication t1
LEFT OUTER JOIN temp_userapplication t2
ON t1.username = t2.username
AND t1.applicationname = t2.applicationname
AND t1.groupname = t2.groupname
SET t1.userdeleted = CASE
WHEN t2.id IS NULL THEN 1
ELSE 0
END,
t1.createddate = CASE
WHEN t2.id IS NOT NULL THEN CURRENT_TIMESTAMP
ELSE t1.createddate
END;
请参阅this fiddle。
发表评论后,如果您只想更新userdeleted
设置为1并且现在设置为0的行:
UPDATE dpuserapplication t1
LEFT OUTER JOIN temp_userapplication t2
ON t1.username = t2.username
AND t1.applicationname = t2.applicationname
AND t1.groupname = t2.groupname
SET t1.userdeleted = CASE
WHEN t2.id IS NULL THEN 1
ELSE 0
END,
t1.createddate = CASE
WHEN t2.id IS NOT NULL AND t1.userdeleted = 1 THEN CURRENT_TIMESTAMP
ELSE t1.createddate
END;
请参阅this fiddle。
在这个小提琴中: