0

If I run the below INSERT statement and there is an autogenerated primary key called "UserID", would it be possible to get the "UserID" of all rows that were just inserted?

INSERT INTO `myTable` (`phone`,`email`,`country`)
VALUES  
("(638) 833-5496","enim.curabitur@hotmail.couk","Spain"),
("(293) 742-0229","odio.semper@yahoo.net","Belgium"),
("1-265-156-4304","tincidunt.dui.augue@outlook.net","Ireland"),
("1-833-780-2553","scelerisque.scelerisque@aol.com","France"),
("(619) 691-0656","ac.risus.morbi@icloud.org","Costa Rica");

Unfortunately, LAST_INSERT_ID() only returns one.

Ken White
  • 123,280
  • 14
  • 225
  • 444
Austin
  • 37
  • 7
  • If phones are uniques, why dont you make a select? – Leandro Bardelli May 21 '22 at 02:07
  • Why not just select max userid before and after your inserts and do select...where userid > maxUserIdBefore and userid<= maxUserIdAfter? – Jonas Metzler May 21 '22 at 02:14
  • @JonasMetzler, that's a race condition. Other sessions may also be inserting rows concurrently. – Bill Karwin May 21 '22 at 03:54
  • Yes, of course, this depends on the setup, was just one idea. – Jonas Metzler May 21 '22 at 03:59
  • Hey all, appreciate the suggestions! Unfortunately, I can't do anything that uses sequential order such as LAST_Insert_ID and Row_Count -1 to grab the range because the way the app works is it will insert into the "user" table if it does not exist but if it does exist, it will still need to insert the existing ID into a different table. I think just using the unique phone number may be my best bet... just need to figure out a way to get those with the way the data is coming in. – Austin May 21 '22 at 05:23

0 Answers0