MySQL UPDATE using IF condition

前端 未结 1 529
独厮守ぢ
独厮守ぢ 2021-01-02 15:27

I\'m not even sure if this is possible but I\'m trying to do different UPDATE if a certain condition is met.

user [id, start_date(NOT NULL), end_date(NULL), reason(N

相关标签:
1条回答
  • 2021-01-02 16:02

    Since the only difference is whether you are setting ur.end_Date to either the current Date or setting it to it's existing date could you not just use the following:

    UPDATE user p  
    JOIN user_roles ur 
    ON p.id = ur.user_id  
    SET 
    ur.end_date = IF (ur.end_date IS NULL, NOW(), ur.end_date),
    p.end_date = NOW(), 
    p.reason = "Retired" 
    WHERE p.id = 5
    
    0 讨论(0)
提交回复
热议问题