查找 2 个日期 SQL 之间的预订 ID (Postgresql)

Find booking Id between 2 dates SQL (Postgresql)

我有 table 次预订,记录如下。

 event_type  |      from_date      |       to_date
-------------+---------------------+---------------------
 party       | 2015-09-24 08:00:00 | 2015-09-24 09:30:00
 hangout     | 2015-09-24 09:00:00 | 2015-09-24 10:00:00
 hangout     | 2015-09-24 10:00:00 | 2015-09-24 10:30:00
 dinner      | 2015-09-24 12:00:00 | 2015-09-24 13:30:00

所以当用户过来 select 前端的日期和时间时,我会发送一个日期和时间作为上面的示例。

现在在后端,我正在考虑使用用户的 selected 日期和时间查询所有记录。

因此如果selected日期和时间存在(查询return大于0)那么我会显示预订不可用,但如果return 0记录,那么可以预订了。

我怎样才能做到这一点?

如果前端给出两个日期和事件类型:

  • fe_period_start
  • fe_period_end
  • fe_event_type

然后查询检查是否没有重叠句点应该是这样的

select count(*) from booking 
 where event_type = fe_event_type
   and from_date <= fe_period_end 
   and to_date >= fe_period_start

在这种情况下,我们还发现预订以 10:00 结束并且我们从 10:00 寻找免费的东西的情况也是一种冲突。

如果我们接受,第一个保留可以在 10:00 结束,第二个可以从 10:00 开始,那么 where 中的不平等一定很严重。

select count(*) from booking 
  where event_type = fe_event_type
    and from_date < fe_period_end 
    and to_date > fe_period_start