MySQL Forums
Forum List  »  Newbie

Re: Help with SQL statement
Posted by: Roland Bouman
Date: August 12, 2005 05:47AM

Well, when you say that fewer than 2 is good too (meanin 1 or 0), this is the easiest and most straightforward:

select user_id
, max(connecttime)
from logins
group by user_id


This will give you the largest connecttime per user_id when for each user that has at least one login. When you need it for all users even if they never had a login this gives the equivalent result:

select u.user_id
, max(l.connecttime)
from users u
left join logins l
on u.user_id = l.user_id
group by u.user_id

so, max(connecttime) will be NULL for those users without logins.

Options: ReplyQuote


Subject
Written By
Posted
August 12, 2005 05:33AM
Re: Help with SQL statement
August 12, 2005 05:47AM
August 12, 2005 06:11AM
August 12, 2005 06:35AM
August 12, 2005 06:52AM
August 12, 2005 05:48AM
August 12, 2005 05:49AM
August 12, 2005 05:50AM
August 12, 2005 05:51AM
August 12, 2005 06:31AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.