Skip to main content
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
add external links. tried to clarify question. i _think_ that's what the OP was getting at? plz revert if no
Source Link

I have the following table (MySQL) with AUTO_INCREMENTan AUTO_INCREMENT column. Now I insert multiple rows with one INSERT.

CREATE TABLE animals ( 
  id MEDIUMINT NOT NULL AUTO_INCREMENT, 
  name CHAR(30) NOT NULL, 
  PRIMARY KEY (id) 
); 
 

Usually I load multiple rows with one INSERT like this:

INSERT INTO animals (name)  
VALUES  ('dog'),('cat'),('cow'); 

Sometimes though I want assign to each value its ID. Like this:

1 -> dog 
2 -> cat 
3 -> cow 

If iI insert simple values - that's ok - iI can select them back. But iwhen I insert JSON documents and it is not good to make SELECT WHERE json IN (...). I even doubt that this will work..JSON documents I'm worried about using syntax like SELECT WHERE json IN (...).

I thought to get MAX(id)MAX(id) before selection, last_insert_idthen get last_insert_id after selection and using count of inserted rows assign id to each. But iI haven't found in documentation anything about order that rows in insert are processed, so i think it's dengerousI'm worried I won't be able to know what the ID's are.

InsertInserting rows one by one and get last_insert_idlast_insert_id for each is not good solution. So i'm tringIs there a way to find something elegant.insert multiple rows at once and return to the application the IDs for each row?

I have table (MySQL) with AUTO_INCREMENT column. Now I insert multiple rows with one INSERT.

CREATE TABLE animals ( 
  id MEDIUMINT NOT NULL AUTO_INCREMENT, 
  name CHAR(30) NOT NULL, 
  PRIMARY KEY (id) 
); 
 
INSERT INTO animals (name) VALUES  ('dog'),('cat'),('cow'); 

I want assign to each value its ID. Like this:

1 -> dog 
2 -> cat 
3 -> cow 

If i insert simple values - that's ok - i can select them back. But i insert JSON documents and it is not good to make SELECT WHERE json IN (...). I even doubt that this will work...

I thought to get MAX(id) before selection, last_insert_id after selection and using count of inserted rows assign id to each. But i haven't found in documentation anything about order that rows in insert are processed, so i think it's dengerous.

Insert rows one by one and get last_insert_id for each is not good solution. So i'm tring to find something elegant.

I have the following table with an AUTO_INCREMENT column.

CREATE TABLE animals ( 
  id MEDIUMINT NOT NULL AUTO_INCREMENT, 
  name CHAR(30) NOT NULL, 
  PRIMARY KEY (id) 
); 

Usually I load multiple rows with one INSERT like this:

INSERT INTO animals (name)  
VALUES  ('dog'),('cat'),('cow'); 

Sometimes though I want assign to each value its ID. Like this:

1 -> dog 
2 -> cat 
3 -> cow 

If I insert simple values I can select them back. But when I insert JSON documents I'm worried about using syntax like SELECT WHERE json IN (...).

I thought to get MAX(id) before selection, then get last_insert_id after selection and using count of inserted rows assign id to each. But I haven't found in documentation anything about order that rows in insert are processed, so I'm worried I won't be able to know what the ID's are.

Inserting rows one by one and get last_insert_id for each is not good solution. Is there a way to insert multiple rows at once and return to the application the IDs for each row?

Source Link
Drewblin
  • 11
  • 1
  • 2

Map auto increment ids on multi-row insert

I have table (MySQL) with AUTO_INCREMENT column. Now I insert multiple rows with one INSERT.

CREATE TABLE animals ( 
  id MEDIUMINT NOT NULL AUTO_INCREMENT, 
  name CHAR(30) NOT NULL, 
  PRIMARY KEY (id) 
); 

INSERT INTO animals (name) VALUES  ('dog'),('cat'),('cow'); 

I want assign to each value its ID. Like this:

1 -> dog 
2 -> cat 
3 -> cow 

If i insert simple values - that's ok - i can select them back. But i insert JSON documents and it is not good to make SELECT WHERE json IN (...). I even doubt that this will work...

I thought to get MAX(id) before selection, last_insert_id after selection and using count of inserted rows assign id to each. But i haven't found in documentation anything about order that rows in insert are processed, so i think it's dengerous.

Insert rows one by one and get last_insert_id for each is not good solution. So i'm tring to find something elegant.