SQL WHERE IN (…) sort by order of the list?

后端 未结 5 1370
一生所求
一生所求 2020-12-21 03:27

Let\'s say I have query a database with a where clause

WHERE _id IN (5,6,424,2)

Is there any way for the returned cursor to be sorted in th

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-21 04:13

    You can join it to a virtual table that contains the list required in sort order

    select tbl.*
    from tbl
    inner join (
        select 1 as sorter, 5 as value union all
        select 2, 6 union all
        select 3, 424 union all
        select 4, 2) X on tbl._id = X.value
    ORDER BY X.sorter
    

提交回复
热议问题