MySQL sort after argument in IN()

前端 未结 4 1287
刺人心
刺人心 2020-12-07 03:55

I have string containing a lot of IDs corresponding to my database. Like:

1,2,3,4,5

I then do a mySQL query to select all those rows:

4条回答
  •  时光取名叫无心
    2020-12-07 04:17

    You might use order by with case ... end, though part between case and end should be generated by PHP:

    select
        *    
    from
        table
    where
        id in (2, 1, 3, 4, 5)
    order by
        case id
            when 2 then 1
            when 1 then 2
            when 3 then 3
            when 4 then 4
            when 5 then 5
        end asc
    

提交回复
热议问题