Home > Database > Mysql Tutorial > decode与case when的区别_MySQL

decode与case when的区别_MySQL

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-01 13:38:09
Original
1597 people have browsed it

bitsCN.com


decode与case when的区别

 

求和:sum(decode(sign(xingji - 6),'',h.real_count,1,h.real_count,0)) 

decode相较于case when,函数里面不能使用and连接多个条件,只能使用函数嵌套 

 

例如:sum(decode(sign(xingji - 6),-1,decode(hh.quxian,18,h.real_count,19,h.real_count,20,h.real_count,24,h.real_count,
34,h.real_count,35,h.real_count,0),0))

求和:sum(case when start_time = '24' then quantity else 0 end) 

case when显得有些臃肿,但是在多个条件连接时比较方便,例如:sum(case when
start_time = '24' and city_code = '010' then quantity else 0 end) 

 

另说明:sum()函数用于求和,sign()函数用于返回参数是大于0,还是小于0或者等于0,
一个例外是返回空:''; 

例如:sign(1)--->1,sign(-1)--->-1,sign(0)--->0,sign(null)--->'';
 

bitsCN.com
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