如何告诉oracle按照从java传入的特定排序顺序进行排序? [英] How to tell oracle to sort by a specific sort order passed in from java?

查看:140
本文介绍了如何告诉oracle按照从java传入的特定排序顺序进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我需要做的事情。

我在java中有一个List,我可以将其转换为逗号单独的ID字符串,如此3,4 ,5,6,1,2

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

我想知道是否有办法将该字符串传递给oracle并根据该字符串的排序顺序进行sql代码排序?

I wonder if there's way to pass that string to oracle and have sql code sort based on that string's sort order?

所以这个查询:

select t.id
from t_test t

会产生此订单

ID
3
4
5
6
1
2


推荐答案

如果你可以在java中修改查询,你可以这样做:

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;

您必须在order by子句中为列表中的每个元素添加解码。每个解码中的第二个参数是列表中的一个元素。

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.

这篇关于如何告诉oracle按照从java传入的特定排序顺序进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆