将时间戳与时间戳范围进行比较

时间:2021-07-19 10:33:42

标签: sql postgresql

我在下一个查询中有 event_timestamp:

 SELECT COUNT(id) = 0 as "isEmpty" 
 FROM status 
 WHERE id = 22 
   AND event_timestamp > user_timestamp

现在我不能使用条件 event_timestamp > user_timestamp,因为 user_timestamp 我有时间戳范围。 喜欢:

id   start_user_timestamp        end_user_timestamp
22   2021-05-02 11:23:59.02343   2021-05-04 21:43:39.02343
22   2021-06-01 13:23:59.02343   2021-06-04 13:43:59.02343
22   2021-07-01 23:43:59.02343   2021-07-04 23:43:59.02343

检查 event_timestamp 是否大于开始/结束值的最佳方法是什么?

1 个答案:

答案 0 :(得分:0)

使用 AND 条件比较两列

SELECT COUNT(id) = 0 as "isEmpty" 
 FROM status 
 WHERE id = 22 
   AND user_timestamp > start_user_timestamp
   AND user_timestamp > end_user_timestamp