SELECT using 'CASE' in SQL

后端 未结 4 1900
失恋的感觉
失恋的感觉 2020-12-16 02:54

I have a set of one to one mappings A -> apple, B-> Banana and like that.. My table has a column with values as A,B,C..

Now I\'m trying to use a select statement whi

相关标签:
4条回答
  • 2020-12-16 02:57

    Try this.

    SELECT 
      CASE 
         WHEN FRUIT = 'A' THEN 'APPLE'
         WHEN FRUIT = 'B' THEN 'BANANA'
         ELSE 'UNKNOWN FRUIT'
      END AS FRUIT
    FROM FRUIT_TABLE;
    
    0 讨论(0)
  • 2020-12-16 03:02

    This is just the syntax of the case statement, it looks like this.

    SELECT 
      CASE 
        WHEN FRUIT = 'A' THEN 'APPLE' 
        WHEN FRUIT = 'B' THEN 'BANANA'     
      END AS FRUIT
    FROM FRUIT_TABLE;
    

    As a reminder remember; no assignment is performed the value becomes the column contents. (If you wanted to assign that to a variable you would put it before the CASE statement).

    0 讨论(0)
  • 2020-12-16 03:08

    Change to:

    SELECT 
      CASE 
        WHEN FRUIT = 'A' THEN 'APPLE' 
        WHEN FRUIT = 'B' THEN 'BANANA'     
      END
    FROM FRUIT_TABLE;
    
    0 讨论(0)
  • 2020-12-16 03:19

    which platform ?

    SELECT 
      CASE 
        WHEN FRUIT = 'A' THEN 'APPLE' 
        ELSE FRUIT ='B' THEN 'BANANA' 
       END AS FRUIT     
    FROM FRUIT_TABLE;
    
    0 讨论(0)
提交回复
热议问题