簡體   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