mysql - joining two tables based on recent logged in datetime -
i working on mysql
, have 2 tables login_users
, login_timestamps
table login_users
keeps record of user_id
, name
, address
whereas table login_timestamps
keeps record of user_id
, timestamp
, table adds new entry each time user logs in, example if user_id
'1' logs in 10 times day, table have 10 entries user_id
'1' today.
now need fetch user profiles based on last logged in time.
for example if there 3 users, mysql
query should give me 3 records latest logged in time.
the query using
select * login_users left join login_timestamps on login_users.user_id = login_timestamps.user_id order login_timestamps.timestamp desc
but gives me previous logged in entries rather recent 1 only.
of course logged in entries while didnt specify when or day or , hete need clause.
try that:
select * login_users left join login_timestamps on login_users.user_id = login_timestamps.user_id date(`timestamp`) = curdate() order login_timestamps.timestamp desc
this give entries curent day. of course can specify condition want.
edit: comment.
try that
select l.user_id, max(timestamp) lasttime login_users l left join login_timestamps lt on l.user_id = lt.user_id group l.user_id order lasttime desc
Comments
Post a Comment