0

我正在使用Postgres数据库构建应用程序,其中涉及比较计划。我使用knex将它连接到节点;但如果解决方案最好由原始查询提供,我也可以使用该路线。PostGres/Knex如何获取两个时间戳之间包含指定时间段的所有条目

我有一个名为"Schedules"的表,它包含一个"from_time""to_time"

我希望能够给予分贝"start_time""end_time",发现:

  1. ,可以包含"start_time"和“END_TIME”所有日程安排(即from_time <= start_time && end_time >= to_time
  2. 所有重叠的"start_time"时间表和"end_time"(即, (start_time <= from_time && end_time > from_time) || (start_time < to_time && end_time >= from_time))

的一种可能的解决方案,我认为是简单地storin g值作为Unix时代的整数......这是我的备份计划。但是,由于这些确实是时间值,因此最好将它们保留为时间戳值格式。

+0

做'from_time'和'to_time'代表*时间戳*或*倍*?如果他们代表时间,是否有意义跨过00:00时间? (即:时间表从22:00开始,并于次日02:00结束)。 – joanolo

+0

当你得到结果时:你是否需要区分'overlapps'和'是否包含在'中(这只是'overlapps'的一个特例)? – joanolo

+0

看看http://dbfiddle.uk/?rdbms=postgres_9.6&fiddle=fbf5fe4c8b5611d92f87a7766f53cc4b,这是你在找什么? – joanolo

回答

0

这一个是你的第一个条件

knex.select('*').from('Schedules').where('from_time', '<=', start_time).where('to_time', '<=', end_time) 

这一个是你的第二个

knex.select('*').from('Schedules').where(function() { 
    this.where('from_time', '>=', start_time).where('from_time' ,'<', end_time)) 
}).orWhere(function() { 
    this.where('to_time', '>', start_time).where('from_time' ,'<=', end_time)) 
}) 
相关问题