I have a list of users. I collect their activity. I want to get a list of users, but I want to order it by who logged in last. Some users haven't logged in in months and don't appear in the login table. There are two tables. One is a users table and the other is a loginTable. The login time is recorded by a timestamp. Here is one way of getting the list and ordering by the last login.
SELECT users.id, MAX(loginTable.timeIn), loginTable.userId FROM loginTable
right join users on loginTable.userId = users.id
ORDER BY MAX(loginTable.timeIn) DESC ;
The right join insures that we get all of the users, even those who don't appear in the loginTable. This is a slow query for a large user table, but since we are collecting metrics it doesn't matter.
Hopes this help someone.
No comments:
Post a Comment