Jump to content

Sorting problem... session & time


mancroft

Recommended Posts

Sorting problem... session & time

Hello

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.
Link to comment
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).
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.