MySQL on duplicate key… get existing ID?

后端 未结 1 1070
旧时难觅i
旧时难觅i 2021-01-28 10:56

I\'m using ON DUPLICATE KEY UPDATE to handle duplicate inserts on a table, in order that they are discarded.

In my case it\'s a simple table storing tags:

相关标签:
1条回答
  • 2021-01-28 11:45

    You could add a third column ModifiedDate and use that:

    insert into t(id, tag)
        select id, tag
        on duplicate key update ModifiedDate = now();
    

    This will ensure that an update really occurs, and in turn, that LAST_INSERT_ID() returns a value.

    0 讨论(0)
提交回复
热议问题