在postgres中存储站点的开放时间

jgzswidk  于 2021-07-29  发布在  Java
关注(0)|答案(1)|浏览(328)

我试图在postgres网站上存储开放时间,在阅读了这个链接后,我设法想出了这个表

create table opening_hours 
 (
    store_id  int REFERENCES site (id) NOT NULL,
    day_of_the_week integer NOT NULL,
    open_time    time with time zone not NULL,
    close_time   time with time zone not NULL
);

我有一个问题,现在有些商店在早上6点到第二天凌晨1点之间营业。
作为 time 字段限制为 24:00:00 我可以在剩余的几个小时内向同一个表中添加新行,例如:

store_id,day_of_the_week,open_time,close_time
1,0,'06:00:00','24:00:00'
1,1,'00:00:00','01:00:00'
1,1,'06:00:00','24:00:00'
...

但这似乎太复杂了。
另一种解决方案是创建此表:

create table opening_hours 
 (
    store_id  int REFERENCES site (id) NOT NULL,
    day_of_the_week integer NOT NULL,
    open_time    time with time zone not NULL,
    operating_minutes   integer not NULL
);

并用以下内容填充表:

store_id,day_of_the_week,open_time,operating_minutes
1,0,'06:00:00',500
1,1,'06:00:00',500
...

但是,此查询将使查询确定是否存在存储 now() 打开有点困难,因为我需要在查询时考虑具体的案例,最终我想查询的是:

select (localtime > open_time and localtime < close_time) from opening_hours oh  where store_id = 1 and day_of_the_week = date_part('dow', now())

我考虑的另一件事是,我总是为每个存储存储数据,不管它们是否打开,但是对于某些报告,我需要过滤掉表中定义的时间范围内的事件 opening_hours .
我正在寻找关于将这些数据存储到postgres中的首选方法的建议。

svmlkihl

svmlkihl1#

您可以使用以下逻辑查询打开的存储:

where (open_time < close_time and localtime between open_time and close_time) or
      (open_time > close_time and localtime not between open_time and close_time)

它使用 between 以及 not between 为了简单起见。但是,它们可能无法按您希望的方式处理确切的打开和关闭时间(是否包括这些特定时间?)。

相关问题