Reply
join tables, showing highest result from t2 joined to t1 by id
Old 01-11-2008, 12:24 PM join tables, showing highest result from t2 joined to t1 by id
hiptobesquare's Avatar
Extreme Talker

Posts: 162
Location: London UK
hi

i have a mysql probelm which has me stumped, i wonder if one of you clever people can help.

i have 2 tables - user (contains user's info) and logins(creates a new row everytime a user logs in detailing ip, date, login count etc).

Id like to run a query which displays user info ordered by most recent login, my problem is that when i join these 2 tables together, i get every row created by by the user from the logins table, and i only need to show the most recent one accosiated with userid from the user table.

<!--example user table -->
id username
1 bob
2 sally
3 sarah

<!--example login table -->
id login
1 2004-06-05
3 2005-04-01
2 2006-01-01
3 2007-11-02
2 2007-10-05
2 2007-09-15

<!-- results id like to see -->
id username login
2 sally 2007-09-15
3 sarah 2007-11-02
1 bob 2004-06-05

the query would be along the lines of -

select * from user left join logins on user.userid=(logins.userid where logins.login=most recent) order by logins.login

i hope that this makes sense, i dont think its a particularly hard query but i cant think through it.

thanks in advance
hiptobesquare is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
Old 01-11-2008, 02:18 PM Re: join tables, showing highest result from t2 joined to t1 by id
chrishirst's Avatar
Super Moderator

Posts: 13,588
Location: Blackpool. UK
Code:
SELECT u.id, u.username, MAX(l.login) FROM user AS u LEFT JOIN login AS l ON u.id = l.id GROUP BY l.id ORDER BY l.login DESC
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
Indifference will be the downfall of mankind, but who cares?
Code Samples | People Counting System
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 01-11-2008, 02:19 PM Re: join tables, showing highest result from t2 joined to t1 by id
joder's Avatar
Flipotron

Posts: 6,443
Name: James
Location: In the ocean.
If the query is working add
limit 1
or if that is the first login
desc limit 1
joder is offline
Reply With Quote
View Public Profile
 
Old 01-12-2008, 09:06 AM Re: join tables, showing highest result from t2 joined to t1 by id
hiptobesquare's Avatar
Extreme Talker

Posts: 162
Location: London UK
excellent! thanks very much chrishirst, its about time a learned what grouping was for.
hiptobesquare is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to join tables, showing highest result from t2 joined to t1 by id
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.12209 seconds with 12 queries