Keep order from 'IN' clause

后端 未结 5 1416
花落未央
花落未央 2020-12-11 00:23

Is it possible to keep order from a \'IN\' conditional clause?

I found this question on SO but in his example the OP have already a sorted \'IN\' clause.

My

5条回答
  •  隐瞒了意图╮
    2020-12-11 00:59

    There is an alternative that uses string functions:

    with const as (select ',45,2,445,12,789,' as vals)
    select tr.*
    from TestResult tr cross join const
    where instr(const.vals, ','||cast(tr.somefield as varchar(255))||',') > 0
    order by instr(const.vals, ','||cast(tr.somefield as varchar(255))||',')
    

    I offer this because you might find it easier to maintain a string of values rather than an intermediate table.

提交回复
热议问题