Home > Database > Mysql Tutorial > Oracle中的decode在mysql中的等价实现

Oracle中的decode在mysql中的等价实现

WBOY
Release: 2016-06-07 16:57:53
Original
1076 people have browsed it

mysql支持if 格式:IF(expr1,expr2,expr3)如果expr1是TRUE(expr1lt;gt;;0且expr1lt;gt;;NULL),那么IF()返回expr2,否则它返

mysql支持if
格式:
IF(expr1,expr2,expr3)
如果expr1是TRUE(expr1;0且expr1;NULL),,那么IF()返回expr2,否则它返回expr3。IF()返回一个数字或字符串值
例如
SELECT IF(1输出yes

同时,还支持case
SELECT   CASE   WHEN   1> 0   THEN   "yes "   ELSE   "no "   END;
还是输出yes

来例子说明:

例一
//Oracle中 decode(Emergency,1,'紧急','普通')

//mysql
select a.title,if(a.Emergency=1,'紧急','普通')emergency from already_sign a

Select title,case Emergency when 1 then '紧急' else '普通' End as emergency   from already_sign


例二

oracle的写法
SELECT decode(ttype,1,’a',2,’b',3,’c',’d') FROM taba

可以在mysql里写成
SELECT if(ttype=1, 'a',if(ttype=2,'b', if(ttype =3, 'c', 'd'))) FROM taba

同理
DECODE(hj_bz, 1, '总库入', 2, '总库出',3,'分库出',4,'已开票',5,'已安装',6,'已收款')

if(hj_bz=1,'总库入',if(hj_bz=2,'总库出',if(hj_bz=3,'分库出',if(hj_bz=4,'已开票',if(hj_bz=5,'已安装','已收款')))))

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