Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Ordering Before Grouping

How to attempt?



11:48 pm on May 8, 2004 (gmt 0)

10+ Year Member

In my particular database table, I have many rows which its common element is the `username`, now the most unique and method I wish to order on, is `time` (timestamp). In my code my base query currently is:

SELECT `username` FROM `download_counter` GROUP BY `username` ASC

Which then as expected, on page rows are ordered by username in alphabetical order. What I wish to do is, on page view, rows are ordered by the `time` value. However heres my two problems:

For starters it probably comes to no surprize that when using GROUP BY, the query remembers the first row it comes across and ignores the laters, so if there are three rows, oviously the first one it finds is the earliest of times, which is exact opposite of what I want. My other problem involves my attempt at a query:

SELECT `username` FROM `download_counter` ORDER BY `time` DESC GROUP BY `username`

this brings back:
#1064 - You have an error in your SQL syntax ... near 'GROUP BY `username`

I guess a ORDER cannot be before a GROUP.

But then what do I do? Is there a way maybe to query all results, then after that preform a GROUP?


11:57 pm on May 8, 2004 (gmt 0)

10+ Year Member

SELECT `username` FROM `download_counter` GROUP BY `username` ASC


SELECT `username`,max(`time`) as maxtime FROM `download_counter` GROUP BY `username` ORDER BY maxtime ASC


6:48 am on May 9, 2004 (gmt 0)

10+ Year Member

hey thanks, its workin perfect.

Featured Threads

Hot Threads This Week

Hot Threads This Month