I'm inserting some_data
(a unique key column), and then using the resulting user_id
(the primary key auto-increment column) in a separate statement (not shown)
INSERT IGNORE INTO users (some_data) VALUES ('test');
SELECT LAST_INSERT_ID(); <--- I do stuff with this.
But, of course, if some_data
already exists (happens very frequently), LAST_INSERT_ID()
returns 0. What is the best way to get the user_id
based on the unique key some_data
, in this case? Of course I can do a separate WHERE query, but not sure that is the most efficient.