LLLLLLL Posted August 18, 2017 Share Posted August 18, 2017 This seems like something I've done several times a year for years, but I can't seem to get it. I can't find a solution on Google that works, although the question was asked many times and answered, too. Also, I'm low on sleep right now. Let's say I have two tables: USERS USERS_LOGINS I want to join the tables and get one row per user, with only the latest login row. Assume "UserId" is the pk/fk and there's some sort of LoginTime in the USERS_LOGINS table. So I will get results like: - Brian 8/4 - Mitch 8/3 - Jerry 8/2 I appreciate the help. Quote Link to comment https://forums.phpfreaks.com/topic/304642-join-table-and-get-only-one-row-from-other-table/ Share on other sites More sharing options...
Solution Barand Posted August 18, 2017 Solution Share Posted August 18, 2017 (edited) SELECT u.name , MAX(DATE(ul.login_time)) as latest FROM USER_LOGINS ul INNER JOIN USERS u USING(userid) GROUP BY u.name Edited August 18, 2017 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/304642-join-table-and-get-only-one-row-from-other-table/#findComment-1549907 Share on other sites More sharing options...
LLLLLLL Posted August 24, 2017 Author Share Posted August 24, 2017 But what if I want everything from users (u.*), and just the one column from logins? This seems to be where I'm getting duplicate rows. Quote Link to comment https://forums.phpfreaks.com/topic/304642-join-table-and-get-only-one-row-from-other-table/#findComment-1550136 Share on other sites More sharing options...
requinix Posted August 24, 2017 Share Posted August 24, 2017 Then SELECT u.* , MAX(DATE(ul.login_time)) as latest FROM USER_LOGINS ul INNER JOIN USERS u USING(userid) GROUP BY u.namethough I would GROUP BY u.userid Quote Link to comment https://forums.phpfreaks.com/topic/304642-join-table-and-get-only-one-row-from-other-table/#findComment-1550137 Share on other sites More sharing options...
LLLLLLL Posted August 24, 2017 Author Share Posted August 24, 2017 (edited) Oops, I'm sorry I messed had some extra stuff in a third table that messed up the query. Edited August 24, 2017 by timneu22 Quote Link to comment https://forums.phpfreaks.com/topic/304642-join-table-and-get-only-one-row-from-other-table/#findComment-1550146 Share on other sites More sharing options...
Psycho Posted August 24, 2017 Share Posted August 24, 2017 Create a dynamic table with the user_id and last login time for each user. Use that table as an intermediary to join each user record with the user last login record. SELECT u.*, ul.* FROM users u JOIN ( SELECT user_id, MAX(login_time) as last_login FROM user_logins GROUP BY user_id ) as i ON i.user_id = u.user_id JOIN user_logins ul ON ul.user_id = i.user_id AND ul.login_time = i.last_login Quote Link to comment https://forums.phpfreaks.com/topic/304642-join-table-and-get-only-one-row-from-other-table/#findComment-1550147 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.