问题描述
我想在MySQL中找到一个等效的DECODE函数。它的工作原理如下:
I am trying to find an equivalent of DECODE function in MySQL. It works like this:
Select Name, DECODE(Age,
13,'Thirteen',14,'Fourteen',15,'Fifteen',16,'Sixteen',
17,'Seventeen',18,'Eighteen',19,'Nineteen',
'Adult') AS AgeBracket
FROM Person
DECODE函数将比较Age列的值与13,14,15 ..并返回适当的字符串值'Thirteen','Fourteen'..如果它匹配什么,则返回Adult的默认值。
The DECODE function will compare value of column 'Age' with 13, 14, 15.. and return appropriate string value 'Thirteen', 'Fourteen'.. and if it matches with nothing, then default value of 'Adult' will be returned.
函数在MySQL中可以做这个工作吗?非常感谢。
Any ideas which function in MySQL can do this job? Thanks.
澄清:我同意使用CASE是实现所需结果的一种方法,但我想寻找 / strong>。
CLARIFICATION: I agree using CASE is one way of achieving desired result, but I am rather looking for a function because of performance and other reasons.
推荐答案
您可以使用 IF()
在Oracle中,您将使用 DECODE()
。
You can use IF()
where in Oracle you would have used DECODE()
.
mysql> select if(emp_id=1,'X','Y') as test, emp_id from emps;
这篇关于MySQL相当于Oracle中的DECODE函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!