举例:
oracle:
转换成mysql:实现
在mysql中有decode()是这样解释的:一种是加密,另外一种是比较
在Oracle中:
语法:DECODE(control_value,value1,result1[,value2,result2…] [,default_result]);control _value试图处理的数值。DECODE函数将该数值与后面的一系列的偶序相比较,以决定返回值。
value1是一组成序偶的数值。如果输入数值与之匹配成功,则相应的结果将被返回。对应一个空的返回值,可以使用关键字NULL于之对应
result1 是一组成序偶的结果值。
default_result 未能与任何一个值匹配时,函数返回的默认值。
例如:
selectdecode( x , 1 , ‘x is 1 ’, 2 , ‘x is 2 ’, ‘others’) from dual
当x等于1时,则返回‘x is 1’。
当x等于2时,则返回‘x is 2’。
否则,返回others’。
需要,比较2个值的时候,可以配合SIGN()函数一起使用。
SELECT DECODE( SIGN(5 -6), 1 'Is Positive', -1, 'Is Nagative', 'Is Zero')
同样,也可以用CASE实现:
<ccid_code>SELECT CASE SIGN(5 - 6) WHEN 1 THEN 'Is Positive' WHEN -1 THEN 'Is Nagative' ELSE 'Is Zero' END FROM DUAL |
此外,还可以在Order by中使用Decode。
例如:表table_subject,有subject_name列。要求按照:语、数、外的顺序进行排序。这时,就可以非常轻松的使用Decode完成要求了。
select * from table_subject order by decode(subject_name, '语文', 1, '数学', 2, , '外语',3)(责任编辑:卢兆林)