database - MySQL on duplicate key... get existing ID? -


i'm using on duplicate key update handle duplicate inserts on table, in order discarded.

in case it's simple table storing tags:

  • id (int, pk, ai, unsigned, not null)
  • tag (varchar 25, not null, unique)

this working fine, need retrieve id - either insert id, on successful insert, or existing id, if it's duplicate.

i'm getting insert id = 0 on duplicate key update fires, guess expected behaviour since no insert took place.

is there anyway can existing id, or headed separate read query?

you add third column modifieddate , use that:

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

this ensure update occurs, , in turn, last_insert_id() returns value.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -