MySQL Select WHERE IN given order

前端 未结 7 1948
时光取名叫无心
时光取名叫无心 2020-12-07 20:26

I have something like

SELECT * FROM table WHERE id IN (118,17,113,23,72);

If I just do this it returns the rows in ID ascending order. Is t

7条回答
  •  情话喂你
    2020-12-07 21:15

    You can create a number to sort on based on the id values:

    select *
    from table
    where id in (118,17,113,23,72)
    order by
      case id
        when 118 then 1
        when 17 then 2
        when 133 then 3
        when 23 then 4
        when 72 then 5
      end
    

提交回复
热议问题