码迷,mamicode.com
首页 > 数据库 > 详细

Oracle的sql语句中case关键字的用法

时间:2016-12-21 20:43:59      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:style   cas   双引号   log   acl   查询   table   sel   语句   

例子1:

a)查询列中有"_"开头的列名时,需要用双引号引起来。

b)使用"case"关键字,根据分数直接判断是否及格。

create table tbl_score(
    id NUMBER(4), --id
    name varchar2(30), --名称
    score NUMBER(3), --分数
    otherscore NUMBER(3) --其他分数
);
select t.id as "_id",
       t.name as name,
       case
           when t.score >= 60 then
            ‘及格
           else
            不及格
       end as ‘是否及格‘,
       t.score as ‘分数‘ 
from tbl_score t;

 

例子2:

根据情况返回不同列的值。

同用上面的tbl_score表。

select t.id as "_id",
       t.name as name,
       case
           when t.score is null then
            t.otherscore  --也可返回固定值
           else
            t.score
       end as 分数
  from tbl_score t;

 

Oracle的sql语句中case关键字的用法

标签:style   cas   双引号   log   acl   查询   table   sel   语句   

原文地址:http://www.cnblogs.com/zj0208/p/6208692.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!