drisate Posted July 16, 2009 Share Posted July 16, 2009 Hey guys ... how do you order by something but keep something else first ... select * from table order by ORDER BY MUN_CODE like = '66057', MUN_CODE I know the querry is not working it's just an exemple. In this case you order by MUN_CODE but you keep the 66057 on top Quote Link to comment Share on other sites More sharing options...
rhodesa Posted July 16, 2009 Share Posted July 16, 2009 you should be able to use a case statement like this: SELECT *,(CASE SUBSTR(name,1,1) WHEN 'b' THEN 0 ELSE 1 END) as sorter FROM mytable ORDER BY sorter,name but it might be better to just use two queries Quote Link to comment Share on other sites More sharing options...
akitchin Posted July 16, 2009 Share Posted July 16, 2009 you may want to add LOWER() so that it's a case-insensitive search. SELECT *, IF(LOWER(SUBSTR(name,1,1)) = 'b', 1, 0) AS sorter .... Quote Link to comment Share on other sites More sharing options...
drisate Posted July 16, 2009 Author Share Posted July 16, 2009 Oh wow lol looks complicated ... my querry is already knid of big how do you adapte that to this? <?php $sql = "SELECT * FROM inscriptions WHERE (AGENT_INSCRIPTEUR_1='MT76440' OR AGENT_INSCRIPTEUR_2='MT76440' OR AGENT_INSCRIPTEUR_1='MT76440' OR AGENT_INSCRIPTEUR_2='MT76440') "; if(isset($_GET['MUN_CODE'])){ $sql = $sql . " and MUN_CODE='".$_GET['MUN_CODE']."'"; } if(isset($_GET['GENRE_PROPRIETE'])){ $sql = $sql . " and GENRE_PROPRIETE='".$_GET['GENRE_PROPRIETE']."'"; } if(!isset($_GET['MUN_CODE']) && !isset($_GET['GENRE_PROPRIETE']) && !isset($_GET['prix'])){ $sql = $sql . " ORDER BY MUN_CODE LIMIT $debut,$combienparpage;"; } echo $sql; $sql = mysql_query($sql); ?> the above gives SELECT * FROM inscriptions WHERE (AGENT_INSCRIPTEUR_1='MT76440' OR AGENT_INSCRIPTEUR_2='MT76440' OR AGENT_INSCRIPTEUR_1='MT76440' OR AGENT_INSCRIPTEUR_2='MT76440') ORDER BY MUN_CODE LIMIT 0,10; I wana keep the rows wher MUN_CODE='66057' on top i tryed this but it's returning no results SELECT *,(CASE SUBSTR(MUN_CODE,1,1) WHEN '66057' THEN 0 ELSE 1 END) as sorter FROM inscriptions WHERE (AGENT_INSCRIPTEUR_1='MT76440' OR AGENT_INSCRIPTEUR_2='MT76440' OR AGENT_INSCRIPTEUR_1='MT76440' OR AGENT_INSCRIPTEUR_2='MT76440') ORDER BY as sorter, MUN_CODE LIMIT 0,10; Quote Link to comment Share on other sites More sharing options...
drisate Posted July 16, 2009 Author Share Posted July 16, 2009 nvm got it lol thx guys ;-) You rock! Quote Link to comment Share on other sites More sharing options...
fenway Posted July 18, 2009 Share Posted July 18, 2009 nevermind got it lol thanks guys ;-) You rock! Then please post your solution for everyone's benefit. Quote Link to comment 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.