Jump to content


Photo

Sorting problem... session & time


  • Please log in to reply
2 replies to this topic

#1 mancroft

mancroft
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 16 June 2006 - 11:26 AM

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.

#2 Wildbug

Wildbug
  • Members
  • PipPipPip
  • Advanced Member
  • 1,149 posts

Posted 16 June 2006 - 02:43 PM



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:

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

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).
Twice a day my clock works PERFECTLY!  I can't figure out what's wrong with it.

#3 mancroft

mancroft
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 16 June 2006 - 02:47 PM

Thank you, Wildbug.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users