Вопрос

I have an SQL INSERT statement like this:

INSERT INTO my_tickets( tk_type, tk_qty) VALUES('typeValue1','qtyValue1'),('typeValue2', 'qtyvalue2'),('typeValue3', 'qtyValue3'); SET @lastId = LAST_INSERT_ID();

I realise that LAST_INSERT_ID() will only get the AUTO_INCREMENT id for the first inserted from the list. There will be more lists. Is there a more efficient way of getting each of the last_insert_ids without repeating(with a PHP loop structure) the INSERT queries all over like this:

INSERT INTO my_tickets(tk_type, tk_qty) VALUES('typeValue1', 'qtyValue1'); 
SET @lastId = LAST_INSERT_ID();

INSERT INTO my_tickets(tk_type, tk_qty) VALUES('typeValue2', 'qtyValue2'); 
SET @lastId = LAST_INSERT_ID();

INSERT INTO my_tickets(tk_type, tk_qty) VALUES('typeValue3', 'qtyValue3'); 
SET @lastId = LAST_INSERT_ID();

I want to make other INSERT statements on another table based on these individual last_insert_ids.

Это было полезно?

Решение

There is no way to do this. last_insert_id() reports only on the LAST tuple inserted. It has absolutely no capability of reporting on all IDs generated by a multi-values insert. If you need multiple last ids, you have to run multiple indepedent SINGLE values-set inserts.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top