Jump to content


Photo

**SOLVED** GROUP BY in the order they are defined in WHERE?


  • Please log in to reply
2 replies to this topic

#1 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 09 February 2006 - 08:21 PM

Say I have the following query:
[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']SELECT[/span] * FROM `table` WHERE `field`="c" OR `field`="d" OR `field`="a" GROUP BY `field` [!--sql2--][/div][!--sql3--]
This returns a result set ordered a, c, d. Is is possible to have the result set ordered by how they were defined in the WHERE part: c, d, a? I tried searching the MySQL manual, but as always, I just can't find what I'm looking for in that excuse for a manual...

Any help is greatly appreciated.

#2 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 09 February 2006 - 09:07 PM

It's not exactly a "documented" use of the FIELD() function, but it should work (UNTESTED):

SELECT * FROM `table` WHERE `field`="c" OR `field`="d" OR `field`="a" GROUP BY `field` ORDER BY FIELD(`field`,'c','d','a')

Hope that helps.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#3 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 09 February 2006 - 09:31 PM

[!--quoteo(post=344220:date=Feb 9 2006, 09:07 PM:name=fenway)--][div class=\'quotetop\']QUOTE(fenway @ Feb 9 2006, 09:07 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
It's not exactly a "documented" use of the FIELD() function, but it should work (UNTESTED):

SELECT * FROM `table` WHERE `field`="c" OR `field`="d" OR `field`="a" GROUP BY `field` ORDER BY FIELD(`field`,'c','d','a')

Hope that helps.
[/quote]
Spot on, works a charm! Thank you ever so much :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users