sql - 检查时间范围是否覆盖 Postgres 中的 now()
问题描述
我有一个以office_hours
数据类型命名的列timerange
,它是通过以下方式定义的:
create type timerange as range (subtype = time)
我想编写一个查询来检查是否now()
包含在该时间范围内。我正在寻找的是类似于:
select now() <@ office_hours from users where office_hours is not null;
每当我尝试运行该查询时,我都会收到以下错误消息:
查询时间范围是否包含的正确方法是什么now()
?
解决方案
错误消息已经给出了很好的提示。由于您有一个创建time
范围,因此您需要now()
在检查遏制之前转换时间。
考虑:
select now()::time <@ office_hours from users where office_hours is not null;
create type timerange as range (subtype = time);
select now(), now()::time <@ timerange('[20:00,23:00]') contained;
产量:
| now | contained |
| ------------------------ | --------- |
| 2019-04-05T20:54:13.113Z | true |
推荐阅读
- r - 具有几个值的图例
- kotlin - 春季测试 Kotlin Hamescreat 有匹配器
- rust - 如何使用 rust-polars 读取压缩的 TSV 文件 (*.gtf.gz)?
- reactjs - Reactjs &Typescript:如何显示 API 调用的结果?
- python - 从线性回归绘制逻辑回归
- python - 将多个情节变成动画
- swift - 用swift替换长字典中的字符
- django - 有没有办法拆分上传的文件扩展名并将其显示在不同的 html 元素中?
- c++ - 我想找到以下 C++ 代码的渐近复杂度 f(n)
- javascript - firebase js sdk 查询快照返回 null 或错误