Answer the question
In order to leave comments, you need to log in
How to quickly find the last entry in a table?
Hello!
There is a table with user files, you need to find the last entry of each user:
CREATE TABLE `files` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`userId` int(10) unsigned NOT NULL DEFAULT '0',
...
`uploadAt` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
SELECT MAX(`uploadAt`), * FROM `files` GROUP BY `userId`
SELECT * FROM `files` WHERE `uploadAt` = MAX(`uploadAt`) GROUP BY `userId`
SELECT * FROM `files` AS a WHERE `uploadAt` = (SELECT MAX(`uploadAt`) FROM `files` AS b WHERE b.userId = a.userId) GROUP BY `userId`
Answer the question
In order to leave comments, you need to log in
SELECT `t2`.*
FROM (
SELECT `userId`, MAX(`uploadAt`) AS `uploadAt`
FROM `files`
GROUP BY `userId`
) AS `t1`
JOIN `files` AS `t2` USING (`userId`, `uploadAt`);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question