SQL高级查询--case表达式

case表达式可以在SQL中实现if-then-else型的逻辑,oracle database 9i及以上版本支持case表达式,case工作方式与decode()类似

有两种类型的case表达式:

1. 简单case表达式,使用表达式确定返回值

case search_expression

   when  expression1 then result1

   when  expression2 then result2

   ...

   when  expressionN then resultN

   else default_result

end

 

* search_expression是待求值的表达式

* expression1、expression2、...、expressionN是要与search_expression进行比较的表达式

* result1、result2、...、resultN是(每一个可能出现的表达式所对应的)返回值,如果expression1的值等于search_expression,则返回result1,以此类推

* default_result是无法找到匹配的表达式时的默认返回值

 

例:

select product_id,product_type_id,

case product_type_id

   when 1  then 'book'

   when 2  then 'video'

   when 3  then 'dvd'

   when 4  then 'cd'

   else  'magazine'

end

from products;

 

product_id   product_type_id   caseprod

--------------   ---------------------    -------------

              1                           1    book

              2                           1    book

              3                           2    video

              4                           2    video

              5                           2    video

              6                           2    video

              7                           3     dvd

              8                           3     dvd

              9                           4     cd

             10                          4     cd

             11                          4     cd

             12                                 magazine

 

 

2. 搜索case表达式,使用条件确定返回值

case

   when  condition1 then result1

   when  condition2 then result2

   ...

   when  conditionN then resultN

   else default_result

end

 

* condition1、condition2、...、conditionN是待求值的表达式

* result1、result2、...、resultN是返回值(对应每个可能出现的条件),如果condition 1为真,则返回result1,以此类推

* default_result是当没有为真的条件时返回的默认结果

 

例:

select product_id,product_type_id,

case

   when product_type_id=1  then 'book'

   when product_type_id=2  then 'video'

   when product_type_id=3  then 'dvd'

   when product_type_id=4  then 'cd'

   else  'magazine'

end

from products;

 

product_id   product_type_id   caseprod

--------------   ---------------------    -------------

              1                           1    book

              2                           1    book

              3                           2    video

              4                           2    video

              5                           2    video

              6                           2    video

              7                           3     dvd

              8                           3     dvd

              9                           4     cd

             10                          4     cd

             11                          4     cd

             12                                 magazine

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值