简体   繁体   English

预订日期范围mysql php

[英]booking dates ranges mysql php

Again booking system and dates ranges. 再次预订系统和日期范围。 I've read this topic Comparing date ranges and many others, but can't find answer :( 我已经阅读了本主题比较日期范围和许多其他内容,但是找不到答案:(

Please see SQLFiddle http://sqlfiddle.com/#!2/89b2b/2 请参阅SQLFiddle http://sqlfiddle.com/#!2/89b2b/2

I have booking table, that have ID , ObjectID , PeriodStart and PeriodEnd fields of type DATE in MySQL 我有预订表,在MySQL具有类型为DATE IDObjectIDPeriodStartPeriodEnd字段
My example data looks like this: 我的示例数据如下所示:

(1, 3, '2014-08-07', '2014-08-14'),
(2, 3, '2014-08-19', '2014-08-23'),
(3, 2, '2014-08-13', '2014-08-15'),
(4, 2, '2014-08-19', '2014-08-21');

My query dates interval is ( updated , was $from = '2014-08-14'; ): 我的查询日期间隔是( update ,was $from = '2014-08-14'; ):

$from = '2014-08-15';
$to = '2014-08-19'

I'd like to get ObjectID s that are free on query dates interval. 我想获取在查询日期间隔内免费的 ObjectID
Problem is that, object's 2 first reservation ends on 2014-08-15 and we want to make new booking started from 2014-08-15 . 问题是,对象的2首次预订在2014-08-15结束,我们要从2014-08-15开始进行新的预订。 Same thing with ending date 2014-08-19 . 结束日期为2014-08-19同一件事。 Existing reservations start on this day. 现有预订从这一天开始。 It's ok for humans because we calculate date periods as nights , but how to tell same thing to MySQL ? 对于humans这是可以的,因为我们将日期周期计算为nights ,但是如何将相同的内容告诉MySQL

For these variables 对于这些变量

$from = '2014-08-15';
$to = '2014-08-19'

I'd like to get [2, 3] as the result of query. 我想获得[2, 3]作为查询的结果。

For second query 对于第二个查询

$from = '2014-08-14';
$to = '2014-08-19'

I'd like to get only [3] as the result of query, because ObjectID 2 is booked from 13 aug to 15 aug 我只想获取[3]作为查询的结果,因为ObjectID 2的预订时间是8 13 aug15 aug

The full code is: 完整的代码是:

SELECT DISTINCT ObjectID
FROM booking
WHERE ObjectID IN
(
SELECT ObjectID
FROM booking
WHERE PeriodStart <= '2014-08-19' AND PeriodEnd >= '2014-08-14'
)

#

+----------+
| OBJECTID |
+----------+
|        2 |
|        3 |
+----------+

Fiddle: http://sqlfiddle.com/#!2/89b2b/90 小提琴: http ://sqlfiddle.com/#!2/89b2b/ 90

You just have a situation where the end date is not inclusive. 您只遇到结束日期不包括在内的情况。 I would express the query as: 我将查询表示为:

select b.objectid
from booking b
group by b.objectid
having sum(b.PeriodStart < '2014-08-19' and B.PeriodEnd > '2014-08-14') = 0

This counts the number of times there is a booking that covers the nights you care about. 这将计算一次预订涵盖您关注的夜晚的次数。 If there are any, then the object is filtered out. 如果有,则将对象过滤掉。

This also seems working? 这似乎还行吗? am i missing anything in question 我是否有任何疑问

SELECT DISTINCT(ObjectID)
FROM booking
WHERE PeriodStart <= '2014-08-19' AND PeriodEnd >= '2014-08-14'
SELECT DISTINCT ObjectID
  FROM booking
 WHERE PeriodStart <= '2014-08-19' 
   AND PeriodEnd >= '2014-08-14';

http://sqlfiddle.com/#!2/89b2b/15 http://sqlfiddle.com/#!2/89b2b/15

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

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