How to update the value of "last_dt" column when using "UPDATE" or "INSERT IGNORE INTO ~ ON DUPLICATE KEY UPDATE ~" statements
P粉633075725
P粉633075725 2023-09-16 13:54:41
0
1
833

The last_dt column is the date when the data was last changed. It is managed differently than inserted dates. Inserted dates are managed by create_dt.

I only want to change the value of the update date when other columns besides the update_dt column change.

So gpt gave me this:

UPDATE your_table
SET column1 = %s, column2 = %s, column3 = %s, update_date = IF(
    column1 <> %s OR column2 <> %s OR column3 <> %s,
    NOW(), update_date
)
WHERE id = %s

But I think this method is a bit strange. I actually tested it and found an error. It's also possible that I wrote the code wrong.

Is there a better way? Is the answer provided by gpt correct? I think many database administrators have solved the same problem. I ask this question because I can't find a suitable answer.

P粉633075725
P粉633075725

reply all(1)
P粉659518294

I think I have to use this

`date`    TIMESTAMP      NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template