简体   繁体   English

MySQL相当于Oracle中的DECODE function

[英]MySQL equivalent of DECODE function in Oracle

I am trying to find an equivalent of DECODE function in MySQL. It works like this:我试图在 MySQL 中找到 DECODE function 的等价物。它的工作原理如下:

Select Name, DECODE(Age,
       13,'Thirteen',14,'Fourteen',15,'Fifteen',16,'Sixteen',
       17,'Seventeen',18,'Eighteen',19,'Nineteen',
       'Adult') AS AgeBracket
FROM Person

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. DECODE function 会将“年龄”列的值与 13、14、15.. 进行比较,并返回适当的字符串值“十三”、“十四”.. 如果没有匹配,则返回默认值“成人” .

Any ideas which function in MySQL can do this job? MySQL 中的 function 有什么想法可以完成这项工作吗? Thanks.谢谢。

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.澄清:我同意使用 CASE 是实现预期结果的一种方式,但由于性能和其他原因,我宁愿寻找function

您可以在 Oracle 中使用DECODE()地方使用IF() DECODE()

mysql> select if(emp_id=1,'X','Y') as test, emp_id from emps; 
Select Name, 
case 
  when Age = 13 then 'Thirteen'
  when Age = 14 then 'Fourteen'
  when Age = 15 then 'Fifteen'
  when Age = 16 then 'Sixteen'
  when Age = 17 then 'Seventeen'
  when Age = 18 then 'Eighteen'
  when Age = 19 then 'Nineteen'
  else 'Adult'
end as AgeBracket
FROM Person

您可以使用CASE语句……但是,为什么不创建一个表,其中包含年龄介于 0 和 150 之间的整数,写出年龄的 varchar,然后您就可以加入该表

Another MySQL option that may look more like Oracle's DECODE is a combination of FIELD and ELT .另一个可能看起来更像 Oracle 的DECODE MySQL 选项是FIELDELT的组合。 In the code that follows, FIELD() returns the argument list position of the string that matches Age.在下面的代码中, FIELD()返回与 Age 匹配的字符串的参数列表位置。 ELT() returns the string from ELT s argument list at the position provided by FIELD() . ELT()FIELD()提供的位置从ELT的参数列表返回字符串。 For example, if Age is 14 , FIELD(Age, ...) returns 2 because 14 is the 2nd argument of FIELD (not counting Age ).例如,如果Age14FIELD(Age, ...)返回2因为14FIELD的第二个参数(不计算Age )。 Then, ELT(2, ...) returns 'Fourteen' , which is the 2nd argument of ELT (not counting the FIELD() argument).然后, ELT(2, ...)返回'Fourteen' ,这是ELT的第二个参数(不包括FIELD()参数)。 IFNULL returns the default AgeBracket if no match to Age is found in the list.如果在列表中找不到与Age匹配项, IFNULL将返回默认的AgeBracket

Select Name, IFNULL(ELT(FIELD(Age,
       13, 14, 15, 16, 17, 18, 19),'Thirteen','Fourteen','Fifteen','Sixteen',
       'Seventeen','Eighteen','Nineteen'),
       'Adult') AS AgeBracket
FROM Person

While I don't think this is the best solution to the question either in terms of performance or readability it is interesting as an exploration of MySQL's string functions.虽然我不认为这在性能或可读性方面是问题的最佳解决方案,但作为对 MySQL 字符串函数的探索很有趣。 Keep in mind that FIELD 's output does not seem to be case sensitive.请记住, FIELD的输出似乎不区分大小写。 Ie, FIELD('A','A') and FIELD('a','A') both return 1 .即, FIELD('A','A')FIELD('a','A')都返回1

The example translates directly to:该示例直接转换为:

Select Name, CASE Age
       WHEN 13 then 'Thirteen' WHEN 14 then 'Fourteen' WHEN 15 then 'Fifteen' WHEN 16 then 'Sixteen'
       WHEN 17 then 'Seventeen' WHEN 18 then 'Eighteen' WHEN 19 then 'Nineteen'
       ELSE 'Adult' END AS AgeBracket
FROM Person

which you may prefer to format eg like this:您可能更喜欢这样格式化:

Select Name,
       CASE Age
         when 13 then 'Thirteen'
         when 14 then 'Fourteen'
         when 15 then 'Fifteen'
         when 16 then 'Sixteen'
         when 17 then 'Seventeen'
         when 18 then 'Eighteen'
         when 19 then 'Nineteen'
         else         'Adult'
       END AS AgeBracket
FROM Person

you can use if() in place of decode() in mySql as follows This query will print all even id row.您可以使用 if() 代替 mySql 中的 decode() 如下此查询将打印所有偶数 id 行。

mysql> select id, name from employee where id in
-> (select if(id%2=0,id,null) from employee);

If additional table doesn't fit, you can write your own function for translation.如果附加表不适合,您可以编写自己的函数进行翻译。

The plus of sql function over case is, that you can use it in various places, and keep translation logic in one place. sql 函数胜过 case 的好处是,你可以在不同的地方使用它,并将翻译逻辑放在一个地方。

While the other answers work correctly in case there are no NULL values involved, here's a NULL safe answer (because that's what Oracle's DECODE really does):虽然其他答案在不涉及NULL值的情况下可以正常工作,但这里有一个NULL安全答案(因为这就是 Oracle 的DECODE真正做的):

SELECT
  Name, 
  CASE
    WHEN Age <=> 13 THEN 'Thirteen'
    WHEN Age <=> 14 THEN 'Fourteen'
    WHEN Age <=> 15 THEN 'Fifteen'
    ...
  END AS AgeBracket
FROM Person

Try this:试试这个:

Select Name, ELT(Age-12,'Thirteen','Fourteen','Fifteen','Sixteen',
   'Seventeen','Eighteen','Nineteen','Adult','Adult','Adult','Adult',
   'Adult','Adult','Adult','Adult','Adult','Adult','Adult','Adult','Adult','Adult',
   'Adult','Adult','Adult','Adult','Adult','Adult','Adult','Adult','Adult','Adult',
   'Adult','Adult','Adult','Adult','Adult','Adult','Adult','Adult','Adult','Adult',
   'Adult','Adult','Adult','Adult','Adult','Adult','Adult','Adult','Adult','Adult',
   'Adult','Adult','Adult','Adult','Adult','Adult','Adult','Adult','Adult','Adult',
   'Adult','Adult','Adult','Adult','Adult','Adult','Adult','Adult','Adult','Adult',
   'Adult','Adult','Adult','Adult','Adult','Adult','Adult','Adult','Adult','Adult',
   'Adult','Adult','Adult','Adult','Adult','Adult','Adult','Adult','Adult','Adult',
   'Adult','Adult','Adult','Adult','Adult','Adult','Adult','Adult','Adult','Adult',
   'Adult','Adult','Adult','Adult','Adult','Adult','Adult','Adult','Adult','Adult',
   'Adult','Adult','Adult','Adult','Adult','Adult','Adult','Adult','Adult','Adult',
   'Adult','Adult','Adult','Adult','Adult','Adult','Adult','Adult','Adult','Adult') AS AgeBracket FROM Person

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM