D
D
Dmitry2017-10-12 01:42:20
MySQL
Dmitry, 2017-10-12 01:42:20

How to get id when doing UPDATE request?

When executing an INSERT query, you can get the id of the last record through the mysql_insert_id () function, but when executing an UPDATE, the auto-increment mechanism is not affected and the function returns 0. Google on this problem issues dances with a tambourine around SELECT and additional columns. Is there any nice solution to this problem? ...or some other function that I didn't find?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
Boris Korobkov, 2017-10-12
@xDMITRIYx

When executing an INSERT query, you can get the id of the last record using the mysql_insert_id() function

Not true. mysql_insert_id() returns the ID of the inserted record.
If you need a record with the maximum ID - SELECT MAX(id) FROM ...
What do you want to get with UPDATE?

I
Immortal_pony, 2017-10-12
@Immortal_pony

In general, the answer has already been given, but it is incorrect.
That's how you can.

UPDATE /* */ SET `id` = LAST_INSERT_ID(`id`), /* */

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question