SQL 在oracle 11g 中如何选择所有列,以及CASE 语句的结果?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1751856/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 04:27:54  来源:igfitidea点击:

How do you select all columns, plus the result of a CASE statement in oracle 11g?

sqloracleora-00923

提问by Kevin Pauli

I want to select *, and not have to type out all individual columns, but I also want to include a custom column with a case statement. I tried the following:

我想选择 *,而不必输入所有单独的列,但我还想包含一个带有 case 语句的自定义列。我尝试了以下方法:

select *, (case when PRI_VAL = 1 then 'High'
                when PRI_VAL = 2 then 'Med'
                when PRI_VAL = 3 then 'Low'
          end) as PRIORITY
from MYTABLE;

But it is complaining that

但它抱怨的是

ORA-00923: FROM keyword not found where expected

回答by Thorsten

Add an alias for mytable like this:

为 mytable 添加一个别名,如下所示:

select t.*, (case when PRI_VAL = 1 then 'High'
                when PRI_VAL = 2 then 'Med'
                when PRI_VAL = 3 then 'Low'
          end) as PRIORITY
from MYTABLE t;

This is not dependent on any specific Oracle version, not sure about other databases.

这不依赖于任何特定的 Oracle 版本,不确定其他数据库。

回答by David Aldridge

As IronGoofy says, add the table alias.

正如 IronGoofy 所说,添加表别名。

On a different note be aware that there is a handy searched case syntax that would be suitable for your situation:

另一方面,请注意有一个方便的搜索案例语法适合您的情况:

select t.*,
       case PRI_VAL
         when 1 then 'High' 
         when 2 then 'Med' 
         when 3 then 'Low' 
       end as PRIORITY 
from MYTABLE t; 

回答by Venkat Reddy

Do it like this:

像这样做:

select e.*,
case deptno
when 30 then 'High'
when 20 then 'Medi'
when 10 then 'Low'
else 'Very Low'
end case
from emp e order by deptno desc;