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;
相关推荐
2301_7930868721 分钟前
Mysql group by
数据库·mysql
jllllyuz37 分钟前
Spring中的事务是如何实现的
数据库·sql·spring
wangmengxxw1 小时前
Spring-常用注解
java·数据库·spring·注解
m0_747266091 小时前
contentprovider实验+SQLite数据库的实现
数据库·sqlite
TDengine (老段)2 小时前
TDengine 中 TDgp 中添加机器学习模型
大数据·数据库·算法·机器学习·数据分析·时序数据库·tdengine
网安Ruler2 小时前
Web开发-PHP应用&Cookie脆弱&Session固定&Token唯一&身份验证&数据库通讯
前端·数据库·网络安全·php·渗透·红队
编程到天明2 小时前
CTF实战:用Sqlmap破解表单输入型SQL注入题(输入账号密码/username&password)
sql·网络安全·web
zfoo-framework4 小时前
线上redis的使用
数据库·redis·缓存
典孝赢麻崩乐急5 小时前
Redis学习-----Redis的基本数据类型
数据库·redis·学习
CF14年老兵5 小时前
SQL 是什么?初学者完全指南
前端·后端·sql