【问题标题】:Is there possible to use in CASE statement in WHEN more columns?是否可以在 WHEN 更多列中的 CASE 语句中使用?
【发布时间】:2015-04-02 08:55:57
【问题描述】:

我正在尝试写这样的东西:

case column_01
    when column_01 = 5 AND column_02 = 'NO'
    then value
    else value_other
end

有没有可能?

【问题讨论】:

标签: sql oracle case


【解决方案1】:

您为 CASE 表达式混合了两种不同的语法

你可以这样做:

CASE
WHEN column_01 = 5 AND column_02 = 'NO' THEN
  value
ELSE
  value_other

1. simple_case_statement

   CASE [ expression ]

   WHEN condition_1 THEN result_1
   WHEN condition_2 THEN result_2
   ...
   WHEN condition_n THEN result_n

   ELSE result

   END

2。 searched_case_statement

CASE 

   WHEN expression condition_1 THEN result_1
   WHEN expression condition_2 THEN result_2
   ...
   WHEN expression condition_n THEN result_n

   ELSE result

END 

【讨论】:

  • 哦,我明白了!非常感谢! :))
  • 或者:when (column_01, column_02) = ((5, 'NO')) then ...
【解决方案2】:

使用 case 语句,您可以检查一列/变量的值并针对该值进行检查:

case column_01
  when 1 then 'a'
  when 2 then 'b'
  ...
  else 'zzz'
end

或者你检查每个 when 子句中的条件:

case when column_01 = 5 and column_02 = 'NO' then value
     when column_01 = 10 and column_03 = 'FRED' then 123
     else value_other
end

你不能将这两种形式结合起来,这样做也没有意义。

【讨论】:

    【解决方案3】:

    没有。你必须为每一列写一个case语句。

    【讨论】:

      猜你喜欢
      • 2015-09-17
      • 1970-01-01
      • 2017-01-16
      • 2015-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-19
      • 2022-08-17
      相关资源
      最近更新 更多