How do I get the ID of multiple inserted rows in MySQL?
56,346
Solution 1
You get it via
SELECT LAST_INSERT_ID();
or via having your framework/MySQL library (in whatever language) callmysql_insert_id()
.That won't work. There you have to query the IDs after inserting.
Solution 2
Why not just:
SELECT ID
FROM terms
WHERE term IN ('word1', 'word2', 'word3')
Comments
-
Googlebot over 3 years
I am inserting some words into a two-column table with this command:
INSERT IGNORE INTO terms (term) VALUES ('word1'), ('word2'), ('word3');
How can I get the ID (Primary Key) of the row in which each word is inserted. I mean returning a value like "55,56,57" after executing
INSERT
. Does MySQL have such a response?The term column is
UNIQUE
. If a term already exists, MySQL will not insert it. Is it possible to return the reference for this duplication (i.e. the ID of the row in which the term exists)? A response like "55,12,56".