gpt4 book ai didi

mysql - 重叠预订 SQL

转载 作者:行者123 更新时间:2023-11-30 21:41:21 25 4
gpt4 key购买 nike

<分区>

我的 sql 语句也有类似的问题 Room Booking Query .

如果公寓只有一个预订,则以下查询有效。但是如果一个公寓有不止一个预订,那么这间公寓也会在结果中,尽管它在请求的时间范围内不可用。

SELECT DISTINCT `apartment`.*
FROM `apartment` `apartment`
LEFT JOIN `booking` `booking` ON `apartment`.`uid` = `booking`.`apartment`
WHERE (
NOT(
( `booking`.`start` <= '2018-07-23')
AND
( `booking`.`end` >= '2018-07-21')
)
)

有人可以帮我写正确的 sql 吗?

更新:根据 Matt Raines 的提示,我在预订表中添加了一个带有公寓 uid 的现场公寓。我非常感谢任何帮助我编写正确的 SQL 语句的建议!

这里是更新演示数据:

--
-- Table structure for table `apartment`
--
CREATE TABLE `apartment` (
`uid` int(11) NOT NULL,
`title` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`bookings` int(10) UNSIGNED NOT NULL DEFAULT '0'
)
--
-- Data for table `tx_apartments_domain_model_apartment`
--
INSERT INTO `apartment` (`uid`, `title`, `bookings`) VALUES
(1, 'Apartment 1', 2),
(2, 'Apartment 2', 1),
(3, 'Apartment 3', 1),
(4, 'Apartment 4', 1);

--
-- Table structure for table `booking`
--
CREATE TABLE `booking` (
`uid` int(11) NOT NULL,
`start` date DEFAULT '0000-00-00',
`end` date DEFAULT '0000-00-00',
`apartment` int(10) UNSIGNED NOT NULL DEFAULT '0'
)
--
-- Data for table `booking`
--
INSERT INTO `booking` (`uid`, `start`, `end`, `apartment`) VALUES
(1, '2018-07-18', '2018-07-20', 1),
(2, '2018-07-21', '2018-07-23', 1),
(3, '2018-07-18', '2018-07-20', 2);

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com