在 SQL 中使用“CASE"进行 SELECT [英] SELECT using 'CASE' in SQL

查看:46
本文介绍了在 SQL 中使用“CASE"进行 SELECT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组一对一的映射 A -> apple, B-> Banana 等等..我的表有一列值为 A、B、C..

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..

现在我正在尝试使用一个 select 语句,它会给我直接的结果

Now I'm trying to use a select statement which will give me the direct result

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

但是我没有得到正确的结果,请帮助我..

But I'm not getting the correct result, please help me..

推荐答案

这只是 case 语句的语法,看起来像这样.

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;

提醒一下;不执行赋值,该值成为列内容.(如果你想把它赋给一个变量,你可以把它放在 CASE 语句之前).

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).

这篇关于在 SQL 中使用“CASE"进行 SELECT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆