问题描述
是否可以保持'IN'条件子句的顺序?
Is it possible to keep order from a 'IN' conditional clause?
我发现这个问题因此,但在他的示例中,OP已经有一个已排序的"IN"子句.
I found this question on SO but in his example the OP have already a sorted 'IN' clause.
我的情况不同,'IN'子句的顺序是随机的像这样的东西:
My case is different, 'IN' clause is in random orderSomething like this :
SELECT SomeField,OtherField
FROM TestResult
WHERE TestResult.SomeField IN (45,2,445,12,789)
我想按(45,2,445,12,789)顺序检索结果.我正在使用Oracle数据库.我可能在SQL中有一个属性,可以与条件子句一起使用以指定该子句的顺序.
I would like to retrieve results in (45,2,445,12,789) order. I'm using an Oracle database. Maybe there is an attribute in SQL I can use with the conditional clause to specify to keep order of the clause.
推荐答案
除非使用ORDER BY子句,否则不会有可靠的排序.
There will be no reliable ordering unless you use an ORDER BY clause ..
SELECT SomeField,OtherField
FROM TestResult
WHERE TestResult.SomeField IN (45,2,445,12,789)
order by case TestResult.SomeField
when 45 then 1
when 2 then 2
when 445 then 3
...
end
您可以将查询分为5个查询并集在一起……
You could split the query into 5 queries union all'd together though ...
SELECT SomeField,OtherField
FROM TestResult
WHERE TestResult.SomeField = 4
union all
SELECT SomeField,OtherField
FROM TestResult
WHERE TestResult.SomeField = 2
union all
...
我更相信前一种方法,它的性能可能会好得多.
I'd trust the former method more, and it would probably perform much better.
这篇关于保持"IN"子句的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!