unxposed Posted November 7, 2008 Share Posted November 7, 2008 I have a mysql query where I'm selecting modules using an array of module id's. These are already in the order which I'd like the rows ordered by, which isn't neccesserily logical (i.e. - could be 24, 2, 7, 1, 3, 15, 10). Buty I just have no idea what to put in the ORDER BY field. $query = "SELECT * FROM modules WHERE mid IN ('".implode("', '", $mids)."') ORDER BY mid ?? "; Many thanks! Link to comment https://forums.phpfreaks.com/topic/131842-solved-sorting-query-in-order-of-id-array/ Share on other sites More sharing options...
Barand Posted November 7, 2008 Share Posted November 7, 2008 $mids = array (24, 2, 7, 1, 3, 15, 10); $midlist = join (', ', $mids); $query = "SELECT * FROM modules WHERE mid IN ($midlist) ORDER BY FIELD(mid, $midlist) "; Link to comment https://forums.phpfreaks.com/topic/131842-solved-sorting-query-in-order-of-id-array/#findComment-684878 Share on other sites More sharing options...
unxposed Posted November 7, 2008 Author Share Posted November 7, 2008 Thanks this is awesome! Link to comment https://forums.phpfreaks.com/topic/131842-solved-sorting-query-in-order-of-id-array/#findComment-684887 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.