oracle case算法的执行次序

很久之前,刚刚弄明白case when then end 算法。

给大家写个case:

select 
case
when data='1' then "A"
when data ='0' then "B"
end text
 from t1;

但是问题是:本人不太懂这个的处理逻辑。

if data ='1':
    text ='true'
if data ='0':
   text ='false'

还是

if data ='1':
    text ='true'
elif data ='0':
   text ='false'

我尝试了下,似乎是第二个。
也就是说:第一个是最优先的,如果符合第一个条件,就不会第二次处理。

写个C的话,就是case有break;
只要符合第一个规则,就不匹配第二个规则。

然后我又发现:case第一项永远像显示在前面。
和if……elif……的逻辑一致。

我写了一个其他的SQL,模拟case的执行逻辑

select 'A' from t1 when data ='1'
union all
select 'B' from t1 where data not in (select data from t1 when data ='1')
where data ='0'
union all
select 'C' from t1 where data not in (select data from t1 when data ='1' or data ='0')

怎么觉得挺简单的事情越写越复杂……

猜你喜欢

转载自blog.csdn.net/weixin_45642669/article/details/114139010