----- Original Message ----- 
From: "Jalil Feghhi" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Sunday, December 19, 2004 3:37 PM
Subject: Order By When Grouping


> Let's say I have a table w/ three rows: ID, Time, and Name. when I do:
>
First of all, I think you mean to say that your table has 3 *columns* named
ID, Time, and Name. It may also have 3 rows but rows don't have names.

> select Time, Name group by Name Order By Time
>
> MySQL selects one of the Time fields if there are rows with the same
> Name and then does the ordering using this selected field (I am not sure
> what is the logic but it seems it is the one that was inserted first in
> the table). I would like to sort on the original Time field and not the
> one selected by MySQL. For example, if:
>
> ID    Time        Name
> 1      2             X
> 2       3            X
> 3       1            Y
>
> I want to see:
>
> 3      X
> 1      Y
>
> But I get:
>
> 2    X
> 1    Y
>
> Any way to do this?
>
I'm not really clear on what you want in your result. Are you trying to show
the oldest time with the name? In that case, your query should be:

select min(time), name
from mytable
group by name
order by 1;

This query would examine all the rows for a given value of 'name', find the
one with the oldest 'time' value on it, and display that time value
alongside the name. The result set rows would be sorted so that row with the
oldest time would be first and the row with the newest time would be last.

Rhino


-- 
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:    http://lists.mysql.com/[EMAIL PROTECTED]

Reply via email to