sql的where条件中使用case when

场景:

1、使用oracle数据库,数据类型为number,需要正无穷值。

2、数据表中有两个金额值,最大值和最小值, 如10~20, 30 ~40,40以上,数据库中这样设计

id name min max

1 j 10 20

2 v 30 40

3 a 40


问题描述

当金额为参数,通过 金额 > min and 金额 <=最大值 作为sql条件,因为oracle中没有正无穷表示,所以用空来表示无穷大。这样就存在 sql:金额 <= null 的情况。


解决方案:

我们在where条件的后边使用case进行判断

sql 复制代码
select * from t 
where (case when max is not null and monry > min
                 and monry <= max 
              then 1
            when max is null and monry > min
              then 1
           else 0
       end) = 1;
相关推荐
GreatSQL社区13 分钟前
用systemd管理GreatSQL服务详解
数据库·mysql·greatsql
掘根13 分钟前
【MySQL进阶】错误日志,二进制日志,mysql系统库
数据库·mysql
weixin_4383354016 分钟前
基础知识:mysql-connector-j依赖
数据库·mysql
小明铭同学31 分钟前
MySQL 八股文【持续更新ing】
数据库·mysql
Mr_Xuhhh41 分钟前
信号与槽的总结
java·开发语言·数据库·c++·qt·系统架构
Fireworkitte1 小时前
Redis 源码 tar 包安装 Redis 哨兵模式(Sentinel)
数据库·redis·sentinel
qq_339282231 小时前
postgressql 如何修改模式的所有表的所有者
数据库
西岭千秋雪_2 小时前
Redis性能优化
数据库·redis·笔记·学习·缓存·性能优化
极限实验室2 小时前
INFINI Labs 产品更新 | INFINI Console 1.29.6 发布 – 优化监控图表异常毛刺等
数据库·产品