Skip to content
Advertisement

MySQL issue on INSERT … SELECT ON DUPLICATE KEY UPDATE and LAST_INSERT_ID()

In MySQL, I have INSERT ... SELECT ON DUPLICATE KEY UPDATE query as below:

UPDATE: This is the result from above query.

Now I want to create an array of the user_id of either the insert or the update the records.

So, my expecting array should be

I tried it something like this, but it doesn’t work for me. That mean I can get only one id.

So, May I know Is there a way to create an array from the effected user ID of the above query running?

Advertisement

Answer

DEMO:

id | category | value
-: | -------: | ----:
 1 |        1 |    11
 2 |        2 |    22

| @ids_array |
| :--------- |
| 1,2        |
id | category | value
-: | -------: | ----:
 1 |        1 |    11
 3 |        1 |   111
 2 |        2 |  null

| @ids_array |
| :--------- |
| 3,2        |
id | category | value
-: | -------: | ----:
 1 |        1 |  null
 3 |        1 |   111
 2 |        2 |  null
22 |        2 |    22

| @ids_array |
| :--------- |
| 3,2,1,22   |

db<>fiddle here

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement