I have an SQL table that is written to by several external users. Each user has its own id and logs a single message at a time. Each row in this table has an auto-increment field called message_id. In other words, each row in the table has its own unique identifier. So if one were to sort the table by the message_id column, he would get all the messages sorted in chronological order.
Is there a single SQL command that can return the latest messages logged by each user?
I can do this in two steps:
- Get a list of user_ids.
For each user_id:
SELECT * FROM myTABLE AS T WHERE T.user_id=user_id ORDER BY message_id DESC LIMIT 1
But I think there is a better way.
Thanks