Valhalla Legends Forums Archive | General Programming | sql GROUP BY clause

AuthorMessageTime
Camel
consider:
[code]SELECT lastlogin, time
FROM whatever
GROUP BY ip;[/code]

when the query returns, each results will be the first row containing each unique ip. is there a way to order the results so that it's the last row containing each unique ip? perhaps "SELECT FROM (SELECT FROM ... ORDER BY time DESC) GROUP BY ip" could work, but is there a simpler way?
June 10, 2003, 11:35 PM
Grok
[quote author=Camel link=board=5;threadid=1607;start=0#msg12044 date=1055288146]
consider:
[code]SELECT lastlogin, time
FROM whatever
GROUP BY ip;[/code]

when the query returns, each results will be the first row containing each unique ip. is there a way to order the results so that it's the last row containing each unique ip? perhaps "SELECT FROM (SELECT FROM ... ORDER BY time DESC) GROUP BY ip" could work, but is there a simpler way?
[/quote]

Yes. You're looking for a self join. I'll just give you a hint for now. Use inner join and the max(time) function and put IP in the where clause. Try that and if you aren't having fun I'll show you how.

Oh and I'm hoping your 'time' is really a datetime. But I suspect lastlogin is the date, and time is the time-of-day. That's going to make it more difficult but you can use the convert() function to solve that in your answer.
June 11, 2003, 2:55 AM
Camel
time is a timestamp. i find the 'date' and 'time' types rather agrivating :)
lastlogin is actually a string (basicly i'm trying to display the last login name that ip used)
anyways, i'll try that
June 11, 2003, 10:03 PM
Camel
ok i give up o_O
June 16, 2003, 2:39 AM
Grok
To display the last login name by IP.

1) If the table contains only the last login for an IP:

SELECT ip, lastlogin, time FROM T1 ORDER BY time

2) If the table contains all logins + times for an IP:

select a.ip, a.lastlogin, a.time
from t1 a inner join (select ip, max(time) as time from t1 group by ip) b
on a.ip = b.ip
where a.time = b.time

Ugly but it works. I'm sure there's a simpler way to do it, but that's all the comes to mind at the moment.

P.S. Your time is not a timestamp, it's a datetime. The name is important because timestamp is an sequence tracking mechanism that you should not use normally. A datetime is what you want.
June 16, 2003, 2:31 PM
Camel
grok, the whole point of asking was because mysql will *not* let you select from a select. unless i'm horribly mistaken, what you posted is near useless to me.


[edit] me fail english? that's unpossible!
June 17, 2003, 2:33 AM
herzog_zwei
MySQL has had (limited) subselects for about a year or so.
June 17, 2003, 2:38 AM
Camel
care to explain? i've had zero success
June 17, 2003, 2:51 AM
herzog_zwei
I'm just going by what the manual for MySQL says. I haven't had the need to use MySQL for some time but I check it from time to time to see how it's matured.

[quote]
Support for subqueries and derived tables

* Subqueries are now supported. Here is an example:

SELECT * FROM t1 WHERE t1.a=(SELECT t2.b FROM t2);

SELECT * FROM t1 WHERE (1,2,3) IN (SELECT a,b,c FROM t2);

* Derived tables (unnamed views) are now supported. Basically, a derived table is a subquery in the FROM clause of a SELECT statement. Here is an example:

SELECT t1.a FROM t1, (SELECT * FROM t2) t3 WHERE t1.a=t3.a;
[/quote]
June 17, 2003, 4:18 AM
Grok
[quote author=herzog_zwei link=board=5;threadid=1607;start=0#msg12478 date=1055817529]
MySQL has had (limited) subselects for about a year or so.

[/quote]

omg. how does anyone get any real work done with it? why are these fools going on and on about MySQL if it's so handicapped?

even MS-Access can select from select, and i wouldn't think of using that for a distributed production database for more than 10 users.
June 17, 2003, 10:58 AM
Camel
[quote author=Grok link=board=5;threadid=1607;start=0#msg12505 date=1055847497]
omg. how does anyone get any real work done with it? why are these fools going on and on about MySQL if it's so handicapped?

even MS-Access can select from select, and i wouldn't think of using that for a distributed production database for more than 10 users.
[/quote]

it's pure php that doesn't overlap with *anything* else. i use ms access (via odbc) as well, but not for this specific case.
June 17, 2003, 8:47 PM

Search