Getting all LAST_INSERT_ID of MySQL package INSERT ... ON DUPLICATE UPDATE ...?

Current lines in t1:

id,f1,f2,f3
1,'01','02','03'
2,'11','12','13'
4,'41','42','43'

Here f1is a unique key field.

Now we execute this query:

INSERT INTO `t1` (`f1`, `f2`, `f3`) VALUES 
('11', '12', '13'),
('21', '22', '23'),
('31', '32', '33'),
('41', '42', '43'),
('51', '52', '53')
ON DUPLICATE KEY UPDATE id = LAST_INSERT_ID(id)

How to get the latest insert identifiers of all inserted / updated rows?

In the above example, I believe the identifiers are:

2 5 6 4 7

Right?

But how can I get them from MySQL after an INSERT batch request with the ON DUPLICATE KEY UPDATE clause as follows?

From this answer , LAST_INSERT_ID "gives the identifier of the FIRST string inserted in the last batch", and you can simply get all the other identifiers in a sequential way if the rows are all fresh INSERT-ed.

-, DUPLICATE, . , INSERT ON DUPLICATE KEY UPDATE? Google, .

t1 t2. " ", t1_x_t2 t2 , t1.

?

+4

Source: https://habr.com/ru/post/1599058/


All Articles