SQL Techniques
Jump to navigation
Jump to search
A page listing some useful SQL techniques.
Sorting based on an array
Suppose you have an array of ids: (12,5,68,93). This array is exactly sorted in PHP as you want, and you need to retrieve these rows on the same order as in the array. You can do that with the FIELD function. This would give the following SQL query:
SELECT * from MyTable WHERE id IN (12,5,68,93) ORDER BY FIELD (id,12,5,68,93);