MySQL
Preserving ordering with WHERE IN() clause in MySQL
Today I ran into the code that pulls from database list of elements with SELECT * FROM table WHERE id IN(2,5,3,16,22,56,48) where order of ids is important to be preserved after results found. When using SELECT this way you will get the results for every id but ordered by id in ascending order. I was looking around and found neat solution to make MySQL work for you to preserve the order of returned results. With ORDER BY FIELD clause like here SELECT * FROM table WHERE id IN (1,5,2,13,4) ORDER BY FIELD(id, 1,5,2,13,4) you can keep the initial ordering.