How to tell oracle to sort by a specific sort order passed in from java?

前端 未结 5 684
长情又很酷
长情又很酷 2021-02-04 18:32

Here\'s what I need to be able to do.

I have a List in java which I can convert to comma separate string of IDs like so \"3,4,5,6,1,2\"

I wonder if there\'s way

5条回答
  •  轮回少年
    2021-02-04 19:13

    Something like this:

    with ordered_ids as (
      select to_number(regexp_substr ('3,4,5,6,1,2','[^,]+',1,level)) as id, level as sort_order
      from dual
      connect by regexp_substr ('3,4,5,6,1,2','[^,]+',1,level) is not null
    )
    select t.id
    from t_test t 
      join ordered_ids oi on oi.id = t.id
    order by oi.sort_order;
    

    You can probably make the literal '3,4,5,6,1,2' a parameter for a PreparedStatement but I haven't tested that.

提交回复
热议问题