使用Oracle中的Select Satement中的Case返回多个列

Din*_*nne 6 oracle

我有一个sceanrio,我需要根据主select语句中的条件从不同的子查询中检索值.我试图使用Case,但问题是Case不支持多列.有什么工作可以解决这个问题,还是有其他办法来实现这个目标.

我在简化查询中的场景

select col1,col2,
case when col3='E01089001' then 
        (select 1,3 from dual)
    else
        (select 2,4 from dual)
end
from Table1
where col1='A0529';
Run Code Online (Sandbox Code Playgroud)

Dav*_*sta 7

这是编写它的另一种方式,可能会解决有关访问第二个表的次数超过必要的问题.

select col1,col2,
case when col3='E01089001' then 1 else 2 end,
case when col3='E01089001' then 3 else 4 end
end
from Table1, dual
where col1='A0529';
Run Code Online (Sandbox Code Playgroud)

你的例子使用了一个明显简化的子查询,所以这个版本看起来很傻; 没有理由加入DUAL.但在您真实的查询中,您可能会有一个子查询SELECT a, b FROM otherTable WHERE someCondition.因此,您可能希望使用实际的列名而不是数字文字和实际的表名而不是dual,并将子查询条件移动到最终的WHERE子句中.