Cian W Cian W - 3 months ago 7
MySQL Question

Writing a query that returns the latest row related to a specific user

Trying to create a query that gets the most recent record related to the user. Here's my attempt

"SELECT *
FROM (
SELECT *
FROM mytable
ORDER BY id
DESC LIMIT 1)
WHERE userid = $userID";

Answer

You could also:

Select * from table 
Where id =
   (Select Max(Id) from table
    where userid = $userID)
Comments