您的位置:首页 > 数据库 > Oracle

oracle中的case when

2015-10-12 17:51 483 查看

关于oracle的的case when 的使用,目前我了解的是如下两种方式

第一种:

<span style="font-size:18px;"><span style="font-size:18px;">select 表中字段,
case
when expression1 then result1
</span><pre name="code" class="plain"><span style="font-size:18px;">  when expression2 then result2</span></span>
...  

<span style="font-size:18px;"><span style="font-size:18px;">  end [也可以给该列加一个名字,但是不能有引号]</span></span>
<span style="font-size:18px;"><span style="font-size:18px;">  from TABLE</span></span>

比如:

<span style="font-size:18px;"><span style="font-size:18px;">select t.*,
case
when t.tgrade<60 then '不及格'
when t.tgrade<span style="font-family: Arial, Helvetica, sans-serif;">>60 and t.tgrade</span><span style="font-family: Arial, Helvetica, sans-serif;"><80  '一般'</span>
when t.tgrade>80 and t.tgrade<100 then '优秀'
end 等级
from t_test t </span></span>




第二种:

跟第一种差不多,只是在case后加了一个表中的字段,作为判断

<span style="font-size:18px;"><span style="font-size:18px;">select 表中字段,
case  表的字段
when value1 then result1
</span><pre name="code" class="plain"><span style="font-size:18px;">  when value2<span style="font-family: Arial, Helvetica, sans-serif;">  </span>then result2</span></span>
...  


<span style="font-size:18px;"><span style="font-size:18px;">  end [也可以给该列加一个名字,但是不能有引号]</span></span>
<span style="font-size:18px;"><span style="font-size:18px;">  from TABLE</span></span>
比如:

<span style="font-size:18px;">select t.*,
case t.tgrade
when 99 then '优秀'
when 48 then '倒数第一'
end
from t_test t;</span>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  oracle