in reply to mysql sorting

Somewhat off topic, but it sounds like you need to look at the group by clause.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.

Replies are listed 'Best First'.
Re^2: mysql sorting
by jhourcle (Prior) on Jun 01, 2005 at 12:26 UTC

    Actually, group by does very little in this case. Sort would work just fine for this situation (see monarch's answer ... that's the type of logic that I would use.)

    In these situations, I think the logic outside of the SQL is more important for the final presentation than something you can do through SQL.

    So, if 'group by' isn't useful here, what is it really good for? Well, it's good for when you're trying to aggregate records, such as the following.

    SELECT groupfield, SUM(somedata) FROM tablename GROUP BY groupfield

    or

    SELECT groupfield, MAX(somedata) FROM tablename GROUP BY groupfield

    In these situations, you only get one row returned per 'group'. There's also the rarely used (in my experience) HAVING clause.

      GROUP BY comes in very handy here, actually. (with MySQL's handy GROUP_CONCAT() function, versions 4.1 and up):
      SELECT Group, GROUP_CONCAT(SomeData ORDER BY SomeData SEPARATOR ',') FROM table GROUP BY `Group`
      In the OP's example, this would result in the following data that would be trivial to loop through and split on the separator chosen in your SQL:
      green 3,9 orange 8 purple 4 red 1,5,6,7 yellow 2
      -- Brian