sql - 在 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 |
有没有办法解决。使用任何其他方法都会产生相同的结果。
解决方案
看来您想要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)
推荐阅读
- javascript - 如何在运行时安装反应包
- node.js - NodeJS:文件已导入,但未执行
- python - 如何使用 graph_objects 子模块在 plotly sunburst 图表上设置颜色?
- python - 在 Pandas 中将过采样数据集保存为 csv 文件
- c++ - 尝试使用 C++ 连接到 mysql 时获取“对 `mysql_init' 的未定义引用”
- facebook-graph-api - 使用图形 API 按关键字搜索 facebook 群组
- javascript - Express Validator 和 Express 是互斥还是依赖包
- jwilder-nginx-proxy - docker nginx-proxy 请求让我们加密证书命中率限制
- python - Statsmodels异方差检验:考虑方差差异旁边的数据权重
- java - Mockito when 和 thenReturn 语句不适用于 Java 中的 2 个方法调用