MySQL equivalent of DECODE function in Oracle

后端 未结 8 2310
南方客
南方客 2020-11-29 09:26

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,\'Fifte         


        
相关标签:
8条回答
  • 2020-11-29 09:29

    Another MySQL option that may look more like Oracle's DECODE is a combination of FIELD and ELT. In the code that follows, FIELD() returns the argument list position of the string that matches Age. ELT() returns the string from ELTs argument list at the position provided by FIELD(). For example, if Age is 14, FIELD(Age, ...) returns 2 because 14 is the 2nd argument of FIELD (not counting Age). Then, ELT(2, ...) returns 'Fourteen', which is the 2nd argument of ELT (not counting the FIELD() argument). IFNULL returns the default AgeBracket if no match to Age is found in the list.

    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. Keep in mind that FIELD's output does not seem to be case sensitive. I.e., FIELD('A','A') and FIELD('a','A') both return 1.

    0 讨论(0)
  • 2020-11-29 09:29

    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 e.g. 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
    
    0 讨论(0)
  • 2020-11-29 09:35
    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
    
    0 讨论(0)
  • 2020-11-29 09:37

    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.

    0 讨论(0)
  • 2020-11-29 09:40

    You can use a CASE statement...however why don't you just create a table with an integer for ages between 0 and 150, a varchar for the written out age and then you can just join on that

    0 讨论(0)
  • 2020-11-29 09:42

    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; 
    
    0 讨论(0)
提交回复
热议问题