Home  >  Q&A  >  body text

How to update the value of "last_dt" column when using "UPDATE" or "INSERT IGNORE INTO ~ ON DUPLICATE KEY UPDATE ~" statements

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粉633075725P粉633075725399 days ago798

reply all(1)I'll reply

  • P粉659518294

    P粉6595182942023-09-17 11:25:31

    I think I have to use this

    `date`    TIMESTAMP      NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,

    reply
    0
  • Cancelreply