在 SQL 中使用“CASE”进行 SELECT
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21313025/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SELECT using 'CASE' in SQL
提问by Vamshi
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..
我有一组一对一的映射 A -> apple, B-> Banana 等等.. 我的表有一列值为 A,B,C..
Now I'm trying to use a select statement which will give me the direct result
现在我正在尝试使用一个 select 语句,它会给我直接的结果
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..
但是我没有得到正确的结果,请帮助我..
回答by Hogan
This is just the syntax of the case statement, it looks like this.
这只是case语句的语法,它看起来像这样。
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).
作为提醒记住; 不执行赋值,该值成为列内容。(如果你想把它分配给一个变量,你可以把它放在 CASE 语句之前)。
回答by everton
Change to:
改成:
SELECT
CASE
WHEN FRUIT = 'A' THEN 'APPLE'
WHEN FRUIT = 'B' THEN 'BANANA'
END
FROM FRUIT_TABLE;
回答by peter.petrov
Try this.
尝试这个。
SELECT
CASE
WHEN FRUIT = 'A' THEN 'APPLE'
WHEN FRUIT = 'B' THEN 'BANANA'
ELSE 'UNKNOWN FRUIT'
END AS FRUIT
FROM FRUIT_TABLE;
回答by venergiac
which platform ?
哪个平台?
SELECT
CASE
WHEN FRUIT = 'A' THEN 'APPLE'
ELSE FRUIT ='B' THEN 'BANANA'
END AS FRUIT
FROM FRUIT_TABLE;