Jump to content


This topic is now archived and is closed to further replies.


Sorting problem... session & time

Recommended Posts

Sorting problem... session & time


Lets say you have data like this:

Time Sessionid
20.55 x3
20.44 x3
20.39 a1
20.33 a1
20.29 x3
20.27 x3

and you want to sort it so it looks like this i.e. grouped according to Sessionid and then Time:

x3 = 20.55 20.44 20.29 20.27
a1 = 20.39 20.33

What is the best way to do this in mySQL?

Thank you.

Share this post

Link to post
Share on other sites

It's easy in MySQL 5.0+ (I think that's when it was added) with the much appreciated GROUP_CONCAT() function. Previous to that function, I'm not sure it's possible in MySQL (you'd have to do it in PHP). Try this:

[code]SELECT sessionid,GROUP_CONCAT(time ORDER BY time DESC SEPERATOR ' ') FROM whatever_table GROUP BY sessionid;[/code]

See [a href=\"http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#id2732618\" target=\"_blank\"]GROUP BY functions[/a] in the manual for more info on options (you can sort the list and specify seperators).

Share this post

Link to post
Share on other sites


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.