jaymc Posted April 29, 2008 Share Posted April 29, 2008 2: when using mysql IN(), for example WHERE id IN(1,2,3,4) if number 4 is the first id in the table, that will be the first returned when using the likes of mysql_fetch_assoc How can I get mysql to return the results in the order I specify in the IN() clause, so in the case above rows 1,2,3,4 Edit: Can this be moved to MYSQL I answered my php question after posting so edited it out Link to comment https://forums.phpfreaks.com/topic/103367-solved-array-and-in/ Share on other sites More sharing options...
Rohan Shenoy Posted April 29, 2008 Share Posted April 29, 2008 For 1. Use the array_pad() function or use the array_reverse(), array_push(), and then again array_reverse() function. Link to comment https://forums.phpfreaks.com/topic/103367-solved-array-and-in/#findComment-529366 Share on other sites More sharing options...
jaymc Posted April 29, 2008 Author Share Posted April 29, 2008 Does anyone know the answer to the MYSQL issue? Link to comment https://forums.phpfreaks.com/topic/103367-solved-array-and-in/#findComment-529542 Share on other sites More sharing options...
rhodesa Posted April 29, 2008 Share Posted April 29, 2008 You can't get them to return in the order of the IN values. But, if they are always in increasing order, you can use an ORDER BY clause: SELECT * FROM tablename WHERE id IN(1,2,3,4) ORDER BY id Link to comment https://forums.phpfreaks.com/topic/103367-solved-array-and-in/#findComment-529544 Share on other sites More sharing options...
PFMaBiSmAd Posted April 29, 2008 Share Posted April 29, 2008 Here are a couple of ways of doing arbitrary ordering (assuming your don't want ascending or descending order) - http://www.shawnolson.net/a/722/mysql-arbitrary-ordering.html http://www.eklekt.com/tutorials/mysql Link to comment https://forums.phpfreaks.com/topic/103367-solved-array-and-in/#findComment-529549 Share on other sites More sharing options...
sasa Posted April 29, 2008 Share Posted April 29, 2008 SELECT * FROM tablename WHERE id IN(1,2,3,4) ORDER BY FIELD(id,1,4,3,2) Link to comment https://forums.phpfreaks.com/topic/103367-solved-array-and-in/#findComment-529576 Share on other sites More sharing options...
rhodesa Posted April 29, 2008 Share Posted April 29, 2008 Wow, I can't believe I've never seen ORDER BY FIELD before...very useful. Link to comment https://forums.phpfreaks.com/topic/103367-solved-array-and-in/#findComment-529579 Share on other sites More sharing options...
jaymc Posted April 29, 2008 Author Share Posted April 29, 2008 SELECT * FROM tablename WHERE id IN(1,2,3,4) ORDER BY FIELD(id,1,4,3,2) Excellent. Link to comment https://forums.phpfreaks.com/topic/103367-solved-array-and-in/#findComment-529588 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.