Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



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

Posts: 186
Location: London UK
Trades: 0
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
 
 
Register now for full access!
Old 01-11-2008, 03:18 PM Re: join tables, showing highest result from t2 joined to t1 by id
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,526
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
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!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 01-11-2008, 03:19 PM Re: join tables, showing highest result from t2 joined to t1 by id
joder's Avatar
Flipotron

Posts: 6,442
Name: James
Location: In the ocean.
Trades: 0
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, 10:06 AM Re: join tables, showing highest result from t2 joined to t1 by id
hiptobesquare's Avatar
Extreme Talker

Posts: 186
Location: London UK
Trades: 0
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 Search this Thread
Search this Thread:

Advanced Search

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

BB 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.14799 seconds with 12 queries