简体   繁体   English

在MySQL中检查日期范围

[英]Checking for date range in MySQL

I am working on a booking system for a beauty salon. 我正在为一家美容院预订系统。 I have tables of: 我有以下表格:

Providers - the workers at the salon. 提供者-沙龙的工人。

TABLE `providers` (
  `providerId` int(11) NOT NULL AUTO_INCREMENT,
  `providerName` varchar(150) NOT NULL DEFAULT '',
  PRIMARY KEY (`providerId`)
)

101 Anna
102 Beata

Services - different services they offer 服务-他们提供的不同服务

TABLE `services` (
  `serviceId` int(11) NOT NULL AUTO_INCREMENT,
  `service` varchar(150) NOT NULL DEFAULT '',
  `serviceDuration` int(11) NOT NULL,
  `providerId` int(11) NOT NULL,
  PRIMARY KEY (`serviceId`)
)

1   Hair cut    30  101
2   Hair coloring   120 101

Schedule - a table over when different services are offered per provider 时间表-有关每个提供商何时提供不同服务的表格

TABLE `schedule` (
  `scheduleId` int(11) NOT NULL AUTO_INCREMENT,
  `serviceId` int(11) NOT NULL,
  `providerId` int(11) NOT NULL,
  `scheduleStartDateTime` datetime NOT NULL,
  `scheduleEndDateTime` datetime NOT NULL,
  PRIMARY KEY (`scheduleId`)
)

1   1   101 2014-10-17 10:00:00 2014-10-17 10:29:59
2   1   101 2014-10-17 10:30:00 2014-10-17 10:59:59
3   1   101 2014-10-17 11:00:00 2014-10-17 11:29:59
4   1   101 2014-10-17 11:30:00 2014-10-17 11:59:59
5   1   101 2014-10-17 12:00:00 2014-10-17 12:29:59
6   2   101 2014-10-17 10:30:00 2014-10-17 12:29:59

And finally, a Bookings - a table of the booked services. 最后,是“预订”-预订服务的表格。

TABLE `bookings` (
  `bookingId` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `clientId` int(11) NOT NULL,
  `bookingDate` datetime NOT NULL,
  `serviceId` int(11) NOT NULL,
  `providerId` int(11) DEFAULT NULL,
  `startDateTime` datetime NOT NULL,
  `endDateTime` datetime NOT NULL,
  PRIMARY KEY (`bookingId`)
)

1   1   2014-10-16  1   101 2014-10-17 10:30:00 2014-10-17 10:59:59

There are of course more tables for Clients, Payments etc that are joined into the searches. 当然,还有更多的客户,付款等表格已加入搜索。

My problem is the search to find the non-booked available services per provider. 我的问题是搜索以查找每个提供商的未预订的可用服务。

Let say someone want a hair cut by Anna. 假设有人想要安娜剪头发。 I then would like to present the available scheduled times for hair cutting, excluding the already booked one at 10:30:00. 然后,我想介绍可用于剪发的预定时间,但不包括10:30:00已预定的时间。

select s.serviceId, s.providerId, s.scheduleStartDateTime, s.scheduleEndDateTime
FROM schedule AS s
WHERE s.serviceId = '1'
AND s.providerId NOT IN
(
    SELECT providerId
    FROM bookings
    WHERE startDateTime <= s.scheduleEndDateTime 
    AND endDateTime >= s.scheduleStartDateTime
)

But this search give this result: 但是此搜索给出以下结果:

1   101 2014-10-17 10:00:00 2014-10-17 10:29:59
1   101 2014-10-17 10:30:00 2014-10-17 10:59:59  <-- This should not show up
1   101 2014-10-17 11:00:00 2014-10-17 11:29:59
1   101 2014-10-17 11:30:00 2014-10-17 11:59:59
1   101 2014-10-17 12:00:00 2014-10-17 12:29:59

http://sqlfiddle.com/#!2/9e5f22 http://sqlfiddle.com/#!2/9e5f22

I think your SQL should be like this : 我认为您的SQL应该是这样的:

 select s.serviceId, s.providerId, s.scheduleStartDateTime, s.scheduleEndDateTime FROM schedule AS s WHERE s.serviceId = '1' AND s.providerId NOT IN ( SELECT b.providerId FROM bookings AS b WHERE b.startDateTime >= s.scheduleEndDateTime AND b.endDateTime <= s.scheduleStartDateTime ) 

The subquery should select the the booked ones, so the main query will exclude these booked. 子查询应选择已预订的,因此主查询将排除已预订的那些。

Anas 阿纳斯

The schedule table seems odd to me. 时间表对我来说似乎很奇怪。 Are you planning to pre-generate rows in the schedule table for each day? 您是否打算每天在计划表中预生成行? If so, then the bookings table would only need to store a schedule id for the booked time slot, but it gets more difficult as services are not all the same length. 如果是这样,那么预订表将只需要存储预订的时隙的时间表ID,但是由于服务的长度不一样而变得更加困难。

It seems like the schedule concept can be simplified. 进度表概念似乎可以简化。 You need to know what time each provider starts and stops their day. 您需要知道每个提供者的开始和结束时间。 Then you can schedule the services at a particular start time, going for the length of the duration (30 minutes or 120 minutes). 然后,您可以在特定的开始时间安排服务,持续时间为30分钟或120分钟。 The start time and duration is what blocks off the providers schedule. 开始时间和持续时间阻碍了提供商的日程安排。

When a customer wants to create a booking, they can choose from the available start times (every 30 minutes) with a long enough duration (ie the openings in the providers schedule). 当客户想要创建预订时,他们可以从可用的开始时间(每30分钟)中选择一个足够长的时间(即提供商计划中的空缺时间)。 The search query can retrieve the existing bookings, and then you can limit the customer's choices on the display side. 搜索查询可以检索现有的预订,然后可以在显示侧限制客户的选择。

I get the correct answer in the sqlfiddle (after changing the wrong date I put in the bookings table). 我在sqlfiddle中得到了正确的答案(更改了我在预订表中输入的错误日期之后)。 I had the same error in my original table. 我在原始表中有相同的错误。 Problem solved! 问题解决了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM