postgresql - Select between with null intervals
问题描述
I want to do something like
SELECT * FROM mytable WHERE created_at BETWEEN A AND B
And these variables can be null, if that is null I wish I could fallback to the lower and upper bound on the data, what I mean:
Name | Created_at |
---|---|
First | 01-01-2021 |
Second | 02-03-2021 |
Second | 31-12-2021 |
A would be 01-01-2021 and B 31-12-2021, or something that has the same meaning. How can I do that?
解决方案
使用 的力量coalesce
:
SELECT * FROM mytable
WHERE created_at BETWEEN coalesce(
CAST (A AS timestamp with time zone),
'-infinity'
)
AND coalesce(
CAST (B AS timestamp with time zone),
'infinity'
);
推荐阅读
- javascript - 为什么 Swiper 在 VUEJS 中不适合我?
- macos - 为什么我的mac big sur 允许非root 用户监听80 端口?
- r - R中的MCMCglmm交互图与ggeffects-package或sjPlot-package
- reactjs - React Native:对于 v0.63.4,我应该删除 Podfile 中所有单独的 pod 行吗?
- css - 更改 sliderInput() 范围数字的最小和最大字体颜色
- git - 当我必须在命令行中输入 GitHub 的用户名和密码时,用户名和密码是什么?
- python - 使用 Python Scrapy Pandas 从在线商店抓取表格(CSS 构建)
- python - 由于 TF-IDF 变换后测试集和训练集的特征数量不同而导致的错误
- odeint - 我的 odeint 函数无法解决一个非常简单的微分方程(我的 Python 技能水平也约为 -1/10)
- java - Netbeans 找不到 JavaFX 应用程序类