I got my db and I only want to get my last row’s username.
The database is named servers
and the table is log
:
timestamp | username --------------------+------------ 2020-11-15 05:45:31 | john 2020-11-15 05:45:59 | juliet 2020-11-15 05:49:00 | borat 2020-11-15 05:49:03 | borat
As I mentioned I’m trying to get this:
Output: borat
I tried using:
SELECT MAX(timestamp), username FROM log
But I’m only getting the correct timestamp and not the right username, and my output is also way longer than what I’m expecting.
Output: MAX(timestamp) juliet 2020-11-15 05:49:03
Does anyone know the solution how to filter only my last record ?
Advertisement
Answer
If you’re using mysql as DBMS as php(deleted tag) suggests, then use order your data descendingly by timestamp
value, and only pick the first one by LIMIT
clause assuming ties do not matter
SELECT * FROM log ORDER BY timestamp DESC LIMIT 1