【SQL】SQL中 decode() 函数介绍
主要作用:将查询结果翻译成其他值(即以其他形式表现出来)
decode() 函数的语法:
select
decode(columnname,值1,翻译值1,值2,翻译值2,...值n,翻译值n,缺省值)
From talbename
Where …
//相当于if判断语句
select decode(status,'01',1,'02',2,0) as new
from tablename group by 1;
+---------+
| new |
+---------+
| 1 |
| 2 |
| 0 |
+---------+
解释:
decode(status,‘01’,1,‘02’,2,0):
字段status下值为01返回1,值为02返回2,否则其他返回0