繁体   English   中英

用于搜索日期范围任一侧 X 天的 SQL 预订查询

[英]An SQL booking query to search X days either side of a date range

我有一个 SQL 查询,它根据开始/结束日期查询查找可用的假期。 我现在需要查询原始范围以及那些包含相同天数但在原始范围的任一侧移动 +/- 1 天或最多 7 天的范围,以查看是否可以容纳预订。 我们的想法是实现类似于 Airbnb 按钮的功能,以检查日期选择的任一侧的可用性。 因此,如果原始范围是 2021-06-23 到 2021-06-30,我还想返回可用结果,例如,该范围任一侧的某一天; 2021-06-22 至 2021-06-29 或 2021-06-24 至 2021-07-01。

表布局是:

属性表:

CREATE TABLE `tbl_properties` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `post_id` bigint(20) UNSIGNED NOT NULL,
  `name` char(32) NOT NULL,
  `num_persons` int(11) NOT NULL,
  `num_children` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `properties` (`id`, `post_id`, `name`, `num_persons`, `num_children`) VALUES
(1, 33741, 'House 1', 2, 0),
(2, 31404, 'House 2', 2, 2);

订票表:

CREATE TABLE `tbl_bookings` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `ical_id` bigint(20) UNSIGNED NOT NULL,
  `start_date` datetime NOT NULL,
  `end_date` datetime NOT NULL,
  `archived` tinyint(1) NOT NULL DEFAULT 0
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `tbl_bookings` (`id`, `ical_id`, `start_date`, `end_date`, `archived`) VALUES
(1, 1, '2021-06-23 13:00:00', '2020-06-30 12:00:00', 0),
(1, 1, '2021-06-30 13:00:00', '2020-07-07 12:00:00', 0),
(2, 2, '2021-08-07 13:00:00', '2020-08-17 12:00:00', 0);

我现有的查询查找可用属性如下所示:

SELECT
        DISTINCT(p.post_id)
    FROM
        tbl_properties p
        LEFT JOIN tbl_bookings b ON h.id = b.ical_id
        AND b.archived = '0'
        AND (
            '2021-06-23 13:00:00' BETWEEN b.start_date AND b.end_date
            OR '2021-06-30 13:00:00' BETWEEN b.start_date AND b.end_date
            OR b.start_date BETWEEN '2021-06-23 13:00:00' AND '2021-06-30 12:00:00'
            OR b.end_date BETWEEN '2021-06-23 13:00:00' AND '2021-06-30 12:00:00'
        )
    WHERE
        b.id IS NULL
        AND(
            h.num_persons >= '0'
            AND (
                h.num_children >= '0'
                OR (
                    tr.term_taxonomy_id = '51'
                    AND (
                        h.num_persons + h.num_children
                    ) >= '0'
                )
            )
        )

这将通过 b.id IS NULL 返回日期范围内在正确的预订表中没有条目的可用属性。

我尝试向子句添加更多范围,每个范围移动 X 天,附加到连接,但仅成功减少了返回的属性数量,而不是按预期增加返回的数量。

因此,理想情况下,如果我查询 2021-06-23 到 2021-06-30 的可用性,如果我要在该范围内请求 +/- 7 天,则将返回两个示例属性。

如果有人能指出我正确的方向,我将不胜感激。 我希望我已经设法描述了这个问题而不会造成太多混乱:)

我有一个 SQL 查询,它根据开始/结束日期查询查找可用的假期。 我现在需要查询原始范围以及那些包含相同天数但在原始范围的任一侧移动 +/- 1 天或最多 7 天的范围,以查看是否可以容纳预订。 我们的想法是实现类似于 Airbnb 按钮的功能,以检查日期选择的任一侧的可用性。 因此,如果原始范围是 2021-06-23 到 2021-06-30,我还想返回可用结果,例如,该范围任一侧的某一天; 2021-06-22 至 2021-06-29 或 2021-06-24 至 2021-07-01。

表布局是:

属性表:

CREATE TABLE `tbl_properties` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `post_id` bigint(20) UNSIGNED NOT NULL,
  `name` char(32) NOT NULL,
  `num_persons` int(11) NOT NULL,
  `num_children` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `properties` (`id`, `post_id`, `name`, `num_persons`, `num_children`) VALUES
(1, 33741, 'House 1', 2, 0),
(2, 31404, 'House 2', 2, 2);

订票表:

CREATE TABLE `tbl_bookings` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `ical_id` bigint(20) UNSIGNED NOT NULL,
  `start_date` datetime NOT NULL,
  `end_date` datetime NOT NULL,
  `archived` tinyint(1) NOT NULL DEFAULT 0
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `tbl_bookings` (`id`, `ical_id`, `start_date`, `end_date`, `archived`) VALUES
(1, 1, '2021-06-23 13:00:00', '2020-06-30 12:00:00', 0),
(1, 1, '2021-06-30 13:00:00', '2020-07-07 12:00:00', 0),
(2, 2, '2021-08-07 13:00:00', '2020-08-17 12:00:00', 0);

我现有的查询查找可用属性如下所示:

SELECT
        DISTINCT(p.post_id)
    FROM
        tbl_properties p
        LEFT JOIN tbl_bookings b ON h.id = b.ical_id
        AND b.archived = '0'
        AND (
            '2021-06-23 13:00:00' BETWEEN b.start_date AND b.end_date
            OR '2021-06-30 13:00:00' BETWEEN b.start_date AND b.end_date
            OR b.start_date BETWEEN '2021-06-23 13:00:00' AND '2021-06-30 12:00:00'
            OR b.end_date BETWEEN '2021-06-23 13:00:00' AND '2021-06-30 12:00:00'
        )
    WHERE
        b.id IS NULL
        AND(
            h.num_persons >= '0'
            AND (
                h.num_children >= '0'
                OR (
                    tr.term_taxonomy_id = '51'
                    AND (
                        h.num_persons + h.num_children
                    ) >= '0'
                )
            )
        )

这将通过 b.id IS NULL 返回日期范围内在正确的预订表中没有条目的可用属性。

我尝试向子句添加更多范围,每个范围移动 X 天,附加到连接,但仅成功减少了返回的属性数量,而不是按预期增加返回的数量。

因此,理想情况下,如果我查询 2021-06-23 到 2021-06-30 的可用性,如果我要在该范围内请求 +/- 7 天,则将返回两个示例属性。

如果有人能指出我正确的方向,我将不胜感激。 我希望我已经设法描述了这个问题而不会造成太多混乱:)

暂无
暂无

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

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