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

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

问题描述

我有一组一对一的映射A->苹果,B->香蕉等等. 我的表格中有一列的值为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天全站免登陆