Home > Database > Mysql Tutorial > body text

decode和case when区别?

WBOY
Release: 2016-06-07 16:49:51
Original
2003 people have browsed it

decode只能做等值的,case when可以做区间的,使用范围来进行条件区分,decode能做到的,case when也能做到;效率方面,个人觉得

 

Oracle中的CASE When:

SELECT LogID,UserID,LogTime,Url,Description,(
  case
when OperateType = 0 then '新增'
   when OperateType=1 then '修改'
   else '删除'
  end ) OperationName
FROM LOG

Oracle中的Decode():
·含义解释:
  decode(条件,值1,翻译值1,值2,翻译值2,...值n,翻译值n,缺省值)

SELECT LogID,UserID,LogTime,Url,Description,
  decode(OperateType,0,'新增',1,'修改','删除') OperationName
  FROM LOG

区别:二者在固定比较条件时使用差不多,如OperateType和固定的值0,1,2等作比较。但是如果当比较条件不固定时,如OperateType变成Sarlary时候,对区间0-2000,,2000-5000,5000以上,显然case when会容易很多。

 

 

其实,decode和case when很多时候是可以等效的,选用那个,完全可以由程序员自己的习惯和熟练程度来决定。但值得一提的是注意CaseWhen的两种形态,将三者罗列如下。我想最好的分析方法就是去实际写一个sql测试一下了,这儿就不多说了。

 

CASE expr WHEN comparison_expr1 THEN return_expr1

[WHEN comparison_expr2 THEN return_expr2

WHEN comparison_exprn THEN return_exprn

ELSE else_expr]

END

 

case when的一个变种

CASE

WHEN comparison_expr1 THEN return_expr1

[WHEN comparison_expr2 THEN return_expr2

WHEN comparison_exprn THEN return_exprn

ELSE else_expr]

END

 

 

DECODE(col|expression, search1, result1

[, search2, result2,...,]

[, searchn, resultn,...,]

[, default])

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

Linux-6-64下安装Oracle 12C笔记

在CentOS 6.4下安装Oracle 11gR2(x64)

Oracle 11gR2 在VMWare虚拟机中安装步骤

Debian 下 安装 Oracle 11g XE R2

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

本文永久更新链接地址:

linux

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template