Page 1 of 1

[Solved] Select Order by specific values

Posted: Tue Dec 12, 2006 4:22 am
by CoderGoblin
Hello,

This one has been puzzling me for some time... If you have a table where 'type' column could be 'f','m','t' is there a way to use something like (this doesn't work...)

Code: Select all

SELECT * FROM contacts WHERE cust_id=66 ORDER BY type ('t','f','m');
I know I could have a separate table of sort_order to join to but I was looking for a more generic way . Another example, in this one we have a load of data in regards to distance...

Code: Select all

$_SESSION['locs']=array('loc1'=>distance,'loc2'=>distance);
asort($_SESSION['locs'];
$locs=implode(',',array_keys($_SESSION['locs']));
$sql="SELECT * FROM table WHERE location IN ({$locs}) ORDER BY ({$locs});
Again the SQL doesn't work but hopefully it shows what I am trying to accomplish. In this case a separate table for sort_order isn't really viable as each user needs a different order, possibly several. As a side note, although I use Postgres I am not after only Postgres solutions.

Posted: Tue Dec 12, 2006 4:36 am
by CoderGoblin
Solved it...

Code: Select all

select * from contacts where cust_id=53 order by type<>'t',type<>'f',type<>'m';
Well finding that was interesting....