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

前端 未结 5 626
长情又很酷
长情又很酷 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 18:50

    If you can modify the query in java, you could do something like this:

    SELECT t.id
    FROM t_test t
    ORDER BY DECODE(t.id, 3, 'A', 'B') ASC,
             DECODE(t.id, 4, 'A', 'B') ASC,
             DECODE(t.id, 5, 'A', 'B') ASC,
             DECODE(t.id, 6, 'A', 'B') ASC,
             DECODE(t.id, 1, 'A', 'B') ASC,
             DECODE(t.id, 2, 'A', 'B') ASC;
    

    You have to put a decode in the order by clause for each element in the list. The second parameter in each decode is one element of the list.

提交回复
热议问题