Greetings I have a question: an array of id-shek comes in and they need to be output in order of their location in the array.

SELECT * FROM table WHERE id=45646 OR id=52535 OR id=154876 OR.... 

It is necessary that the sorting be strictly on the order of id, as in the query 45646, 52535, 154876, etc. is it possible And also, I suspect that the request can be somehow changed (not through OR)? Thank.

    1 answer 1

    ... WHERE id IN(45646, 52535, 154876) [ORDER BY][1] id ASC

    Ai, then harder

     SELECT id, CASE id WHEN 45646 THEN 1 WHEN 52535 THEN 2 WHEN 154876 THEN 3 ELSE 0 END AS sort FROM table WHERE id IN(45646, 52535, 154876) ORDER BY sort ASC; 

    Something like that, play.

    • so sorts by ascending id in the table, but as it should in the query ... - valera77
    • still sorts by table ... i.e. for example SELECT * FROM releases WHERE id IN (2, 1, 3, 7, 4) ORDER BY r_id ASC displays 1, 2, 3, 4, 7 - valera77
    • Updated - Sh4dow
    • It works, thanks! - valera77