MySQL on duplicate key… get existing ID?
问题 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: id (int, PK, AI, unsigned, not null) tag (varchar 25, not null, unique) This is working fine, but I need to retrieve the ID - either the insert ID, on successful insert, or the existing ID, if it's a duplicate. I'm getting insert ID = 0 where ON DUPLICATE KEY UPDATE fires, which I guess is expected behaviour since no insert took place. Is