【问题标题】:How do you select all columns, plus the result of a CASE statement in oracle 11g?在 oracle 11g 中如何选择所有列以及 CASE 语句的结果?
【发布时间】:2016-03-14 23:34:41
【问题描述】:

我想选择 *,而不必输入所有单独的列,但我还想包含一个带有 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;

但它在抱怨

ORA-00923: FROM keyword not found where expected

【问题讨论】:

    标签: sql oracle ora-00923


    【解决方案1】:

    像这样为 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;
    

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

    【讨论】:

    • 谢谢!好吧,我想我过度简化了我的问题。如果列是连接的结果,即 SELECT ... FROM MYTABLE M JOIN ANOTHER A ON M.ID = A.ID,该怎么办?
    • 没关系,我刚刚找到了这个加入问题的答案。选择 M.*, A.*, (case ...再次感谢!
    【解决方案2】:

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

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

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

    【讨论】:

      【解决方案3】:

      这样做:

      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;
      

      【讨论】:

        猜你喜欢
        • 2021-02-05
        • 1970-01-01
        • 2012-12-22
        • 2015-11-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-07
        • 1970-01-01
        相关资源
        最近更新 更多