首页 > 解决方案 > 在 case 语句中设置条件

问题描述

假设有这样的表结构

Name | Class | CS_ex_date | IT_ex_date|

xyz  | CSE   | 10 june    |  Null     |
123  | ECE   | Null       |  Null     |
456  | MECH  | Null       |  Null     |
678  | MECH  | Null       |  Null     |
abc  | IT    | Null       | 3 Aug     |

我想使用 case 语句创建一个 select 语句,并且在 case 语句中有一些条件。

我已经在 where 条件中看到了 case 语句和 case 语句的常用语法,但我想在 THEN 部分中放置一些条件。我的语法有问题,找不到任何可以参考的例子。

我的查询是这样的:

select * 
  from student 
 where (case 
          when class like '%SE%' 
            then CS_ex_date > sysdate and CS_ex_date < sysdate + 60
          when class like '%T%' 
            then IT_ex_date > sysdate and IT_ex_date < sysdate + 60 
        end);

我不确定查询的语法和获取 ORA-00905: missing keyword。

预期的输出应该是

Name | Class | CS_ex_date | IT_ex_date|

xyz  | CSE   | 10 june    |  Null    |
abc  | IT    | Null       | 3 Aug    |

有没有办法解决。使用任何其他方法都会产生相同的结果。

标签: sqloracle

解决方案


看来您想要or,而不是case

select * 
  from student 
 where (class like '%SE%' and CS_ex_date > sysdate and CS_ex_date < sysdate + 60) or
       (class like '%T%'  and IT_ex_date > sysdate and IT_ex_date < sysdate + 60)

推荐阅读