K
K
khodos_dmitry2018-02-27 17:31:07
MySQL
khodos_dmitry, 2018-02-27 17:31:07

How to select last updated or added row in mysql database?

Maybe LAST_INSERT_ID() . But for some reason this construction does not work for me:
SELECT * FROM `item` WHERE `id` = LAST_INSERT_ID();

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
ivankomolin, 2018-02-27
@khodos_dmitry

SELECT * FROM item ORDER BY id DESC LIMIT 1
If id is an increment, output the last row added.
If you need to display the last modified line, then you need to add the updated field, in which to write the time of the change. And then, by analogy with the example above, only sorting by modification date.

K
Konstantin Tsvetkov, 2018-02-27
@tsklab

Can without addition of an additional column it is possible to do?

Logs (logs) in MySQL .

N
nozzy, 2018-02-27
@nozzy

SET @lastID := LAST_INSERT_ID();
SELECT * FROM `item` WHERE `id` = @lastID;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question