I am doing multiple inserts in one statement e.g.:
INSERT INTO table (foo) VALUES ('aaa'), ('bbb'), ('ccc')
When I try to get the last insert id, MySQL (PDO) only gives the first insert id.
Since I know how many items I inserted (three items from the example above), is it reliable to manually compute the inserted ids as long as the INSERT
is done as a transaction?
E.g., if the first insert id that MySQL returns is 5
, can I safely assume that the insert ids of the above INSERT
example is 5
, 6
, 7
? Or is there still a possibility of overlap if another user inserts at the same time, even if they were done as a transaction?
The suggested duplicate question, while similar, does not clearly answer my question. The accepted answer there only says it is expected behavior that only the first insert id is given, which I agree, but my question here is about how reliable is the method of the manual computation. The comments section there also looks anecdotal.
question from:
https://stackoverflow.com/questions/65923925/reliability-of-computing-insert-ids-of-a-multi-row-insert-statement 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…