39

I am inserting some words into a two-column table with this command:

INSERT IGNORE INTO terms (term) VALUES ('word1'), ('word2'), ('word3'); 
  1. 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?

  2. 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".

3 Answers 3

37
  1. You get it via SELECT LAST_INSERT_ID(); or via having your framework/MySQL library (in whatever language) call mysql_insert_id().

  2. That won't work. There you have to query the IDs after inserting.

Sign up to request clarification or add additional context in comments.

3 Comments

Right - not in one with INSERT IGNORE. But in a non-IGNORE one, it gives you the first value generated and inserted.
What if another user has made another insert in the meanwhile?
@Pitto Nevertheless you would get the correct value from LAST_INSERT_ID() resp. mysql_insert_id(). The former knows what has last happened in the current session, the latter is directly bound to the last query. If something else happens in a different connection, that doesn't hurt.
1

Why not just:

SELECT ID FROM terms WHERE term IN ('word1', 'word2', 'word3') 

4 Comments

That'd fetch terms that were previously inserted, and it'd be vulnerable to a race condition where something else inserts records before the select query fires.
?? term has a UNIQUE condition. The only thing that could affect the SELECT in a race condition is a DELETE, not another INSERT. And according to (2) in OP's post, they want the previously inserted records' IDs to be included.
That's what I had in mind; but it needs few lines of coding. Apart from SELECT, I need fetch the results and get them in a while loop (in php that I use). I hope to find some kind of mysql response.
@Googlebot this is the best way to go about it. If you want to iterate over the IDs, just execute a SELECT (shown in this answer) and iterate over the results. What's in a few more lines of coding anyway? If you really want MySQL to echo the ID, you have to issue separate single INSERT statements and execute SELECT LAST_INSERT_ID() after each INSERT to collect the IDs into an array.
0

First, to get the id just inserted, you can make something like :

SELECT LAST_INSERT_ID() ; 

Care, this will work only after your last INSERT query and it will return the first ID only if you have a multiple insert!

Then, with the IGNORE option, I don't think that it is possible to get the lines that were not inserted. When you make an INSERT IGNORE, you just tell MySQL to ignore the lines that would have to create a duplicate entry.

If you don't put this option, the INSERT will be stopped and you will have the line concerned by the duplication.

3 Comments

Even without IGNORE, it will only works for single-row INSERT. My command is a multi-row INSERT
Only the first part of my answer was including the IGNORE.
If you're using PHP, you can store the IDs in a table using a loop with a single INSERT in each passage.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.