zq29 Posted February 9, 2006 Share Posted February 9, 2006 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] * [color=green]FROM[/color] [color=orange]`table`[/color] [color=green]WHERE[/color] `field`[color=orange]=[/color][color=red]"c"[/color] [color=blue]OR[/color] `field`[color=orange]=[/color][color=red]"d"[/color] [color=blue]OR[/color] `field`[color=orange]=[/color][color=red]"a"[/color] 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. Quote Link to comment https://forums.phpfreaks.com/topic/3366-solved-group-by-in-the-order-they-are-defined-in-where/ Share on other sites More sharing options...
fenway Posted February 9, 2006 Share Posted February 9, 2006 It's not exactly a "documented" use of the FIELD() function, but it should work (UNTESTED):[code]SELECT * FROM `table` WHERE `field`="c" OR `field`="d" OR `field`="a" GROUP BY `field` ORDER BY FIELD(`field`,'c','d','a')[/code]Hope that helps. Quote Link to comment https://forums.phpfreaks.com/topic/3366-solved-group-by-in-the-order-they-are-defined-in-where/#findComment-11515 Share on other sites More sharing options...
zq29 Posted February 9, 2006 Author Share Posted February 9, 2006 [!--quoteo(post=344220:date=Feb 9 2006, 09:07 PM:name=fenway)--][div class=\'quotetop\']QUOTE(fenway @ Feb 9 2006, 09:07 PM) [snapback]344220[/snapback][/div][div class=\'quotemain\'][!--quotec--]It's not exactly a "documented" use of the FIELD() function, but it should work (UNTESTED):[code]SELECT * FROM `table` WHERE `field`="c" OR `field`="d" OR `field`="a" GROUP BY `field` ORDER BY FIELD(`field`,'c','d','a')[/code]Hope that helps.[/quote]Spot on, works a charm! Thank you ever so much :) Quote Link to comment https://forums.phpfreaks.com/topic/3366-solved-group-by-in-the-order-they-are-defined-in-where/#findComment-11516 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.