How do I preserve the order of a SQL query using the IN command

后端 未结 5 833
天涯浪人
天涯浪人 2020-12-20 22:47
SELECT * FROM tblItems
WHERE itemId IN (9,1,4)

Returns in the order that SQL finds them in (which happens to be 1, 4, 9) however, I want them retur

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-20 23:09

    Use a CASE expression to map the ID values to an increasing sequence:

    ... ORDER BY CASE itemId
                 WHEN 9 THEN 1
                 WHEN 1 THEN 2
                 ELSE        3
                 END
    

提交回复
热议问题