I am working on MySql
and have two tables login_users
and login_timestamps
Table login_users
keeps a record of user_id
, name
and address
whereas table login_timestamps
keeps a record of user_id
and timestamp
, this table adds a new entry each time user logs in, so for example if the user_id
'1' logs in 10 times a day, this table will have 10 entries for user_id
'1' for today.
Now I need to fetch user profiles based on their last logged in time.
for example if there are 3 users, the MySql
query should give me 3 records with their latest logged in time.
The query I am using is
SELECT * FROM login_users LEFT JOIN login_timestamps ON login_users.user_id = login_timestamps.user_id ORDER BY login_timestamps.timestamp DESC
but this gives me all the previous logged in entries rather than the recent one only.
Of course you will get all logged in entries while you didnt specify when or what day or something , hete you need a where clause.
try that:
SELECT * FROM login_users
LEFT JOIN login_timestamps ON login_users.user_id = login_timestamps.user_id
WHERE DATE(`timestamp`) = CURDATE()
ORDER BY login_timestamps.timestamp DESC
this will give you entries for curent day. of course you can specify any condition you want.
EDIT: from your comment.
try that
SELECT l.user_id, max(timestamp) as lasttime
FROM login_users l
LEFT JOIN login_timestamps lt ON l.user_id = lt.user_id
GROUP BY l.user_id
ORDER BY lasttime DESC
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.