设计运行时间SQL表

发布于 2024-12-02 12:55:58 字数 448 浏览 1 评论 0原文

我正在设计一个 SQL 表来存储商店的营业时间。

有些商店的营业时间非常简单:周一至周日上午 9:30 至晚上 10:00

其他商店则稍微复杂一些。请考虑以下场景:

Monday:  Open All Day
Tuesday: 7:30AM – 2:30PM & 4:15PM – 11:00 PM 
Wednesday: 7:00PM – 12:30 AM (technically closing on Thursday morning)
Thursday: 9:00AM – 6:00PM
Friday: closed.

您将如何设计表格?

编辑

营业时间将用于显示商店是否在用户选择的时间营业。

不同的表可能可以处理任何异常,例如假期。

商店的营业时间每周不会改变。

I am designing a SQL table to store hours of operation for stores.

Some stores have very simple hours: Monday to Sunday from 9:30AM to 10:00PM

Others are little more complicated. Please consider the following scenario:

Monday:  Open All Day
Tuesday: 7:30AM – 2:30PM & 4:15PM – 11:00 PM 
Wednesday: 7:00PM – 12:30 AM (technically closing on Thursday morning)
Thursday: 9:00AM – 6:00PM
Friday: closed.

How would you design the table(s)?

EDIT

The hours will be used to showing if a store is open at a user selected time.

A different table can probably handle any exceptions, such as holidays.

The store hours will not change from week to week.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

等待圉鍢 2024-12-09 12:55:58

这样的表格对于您发布的输出来说很容易,并且只需稍微回击一下(打开?是/否):

Store | Day | Open | Closed
---------------------------
1     | 1   | 0000 | 2400
1     | 2   | 0730 | 1430
1     | 2   | 1615 | 2300
...

特点:

  1. 使用 24 小时不是必需的,但使数学更容易。
  2. 商店 ID 可能会连接到您存储商店信息的查找表。
  3. 日期 ID 将转换为星期几(1 = 星期日,2 = 星期一等)

要查询数据集,只需:
SELECT Day, Open, Close... (显然你想要格式化 Open/Close)

要查询 IsOpen?,只需:

SELECT CASE WHEN @desiredtime BETWEEN Open AND Closed THEN 1 ELSE 0 END 
FROM table 
WHERE store = @Store

A table like this would be easy for both the output you posted, as well as just firing a bit back (open? yes/no):

Store | Day | Open | Closed
---------------------------
1     | 1   | 0000 | 2400
1     | 2   | 0730 | 1430
1     | 2   | 1615 | 2300
...

Features:

  1. Using 24-hour isn't necessary, but makes math easier.
  2. Store ID would presumably join to a lookup table where you stored Store information
  3. Day ID would translate to day of week (1 = Sunday, 2 = Monday, etc.)

To query for your dataset, just:
SELECT Day, Open, Close... (you'd want to format Open/Close obviously)

To query IsOpen?, just:

SELECT CASE WHEN @desiredtime BETWEEN Open AND Closed THEN 1 ELSE 0 END 
FROM table 
WHERE store = @Store
泅渡 2024-12-09 12:55:58

更多地将其视为定义时间范围,天/周更复杂,因为它们有规则并定义了开始和停止。

您将如何定义时间范围?

一个约束(开始[时间和日期])一个参考 >“持续时间”(小时、分钟……跨度)*。现在,班次(时间范围)可以跨越多天,您无需使用复杂的逻辑来提取和在计算中使用数据。

**Store_Hours**

Store | Day | Open | DURATION
---------------------------
1     | 1   | 0000 | 24
1     | 2   | 0730 | 7
1     | 2   | 1615 | 6.75
... 
1     | 3   | 1900 | 5.5

Think of it more as defining time frames, days / weeks are more complex, because they have rules and defined start and stops.

How would you define a timeframe?

one constraint (Start[Time and Day]), one reference 'Duration' (hours, minutes,.. of the span)*. Now the shifts (timeframes) can span multiple days and you don't have to work complex logic to extract and use the data in calculations.

**Store_Hours**

Store | Day | Open | DURATION
---------------------------
1     | 1   | 0000 | 24
1     | 2   | 0730 | 7
1     | 2   | 1615 | 6.75
... 
1     | 3   | 1900 | 5.5
泪痕残 2024-12-09 12:55:58

除了存储和展示之外,您还需要做更多的事情吗?

我认为一个需要告诉商店在特定时间是否营业的设计必须了解所有的可能性,否则,你最终将无法容纳某些东西。

假期例外怎么办?

我会考虑将它们存储为基于基准时间的间隔(从一周时间 0 开始的分钟数)。

所以 0 是星期一的午夜。

间隔 1 为 0 - 1440,

间隔 2 为 1890 - 2310

,等等。

您可以轻松地将用户选择的时间转换为分钟偏移量,并确定商店是否营业。

剩下的唯一问题是显示中的解释,以便友好显示(可能是一些广泛的逻辑,但并非不可能)以及时间 10080 -> 的重叠。 0。

Do you have to do more than just store and display it?

I think a design which needs to tell if a store is open at a particular time would have to be informed by all of the possibilities, otherwise, you will end up not being able to accommodate something.

What about holiday exceptions?

I would consider storing them as intervals based on a base time (minutes since time 0 on a week).

So 0 is midnight on Monday.

Interval 1 would be 0 - 1440

Interval 2 would be 1890 - 2310

etc.

You could easily convert a user selected time into a minute offset and determine if a store was open.

Your only problem remaining would be interpretation in display for friendly display (probably some extensive logic, but not impossible) and overlap at time 10080 -> 0.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文