Php 预订并检查是否有空房

Php 预订并检查是否有空房,php,mysql,Php,Mysql,我正在创建一个预订房间的系统。我需要找出日期范围内的可用房间数 房间在结账时应该是可用的 到目前为止,我只能找到日期范围内的订单,但即使这些订单也不能正常工作 本工程于2020年6月27日至2020年7月05日完工 这不起作用2020-06-28-2020-07-05 目标是找出日期范围内的免费房间数量,然后提供订购 我在这里读了很多话题,但我没有找到解决办法 我使用PHP和MySQL -- phpMyAdmin SQL Dump -- version 4.7.4 -- https://www.

我正在创建一个预订房间的系统。我需要找出日期范围内的可用房间数

房间在结账时应该是可用的

到目前为止,我只能找到日期范围内的订单,但即使这些订单也不能正常工作

本工程于2020年6月27日至2020年7月05日完工

这不起作用2020-06-28-2020-07-05

目标是找出日期范围内的免费房间数量,然后提供订购

我在这里读了很多话题,但我没有找到解决办法

我使用PHP和MySQL

-- phpMyAdmin SQL Dump
-- version 4.7.4
-- https://www.phpmyadmin.net/
--
-- Počítač: 127.0.0.1
-- Vytvořeno: Sob 27. čen 2020, 10:24
-- Verze serveru: 10.1.28-MariaDB
-- Verze PHP: 7.2.1

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Databáze: `rezervace`
--

-- --------------------------------------------------------

--
-- Struktura tabulky `booking_orders`
--

CREATE TABLE `booking_orders` (
  `id` int(11) NOT NULL,
  `number` int(11) NOT NULL,
  `checkin` date NOT NULL,
  `checkout` date NOT NULL,
  `first_name` varchar(255) COLLATE utf8_czech_ci NOT NULL,
  `last_name` varchar(255) COLLATE utf8_czech_ci NOT NULL,
  `email` varchar(255) COLLATE utf8_czech_ci NOT NULL,
  `phone` varchar(20) COLLATE utf8_czech_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci;

--
-- Vypisuji data pro tabulku `booking_orders`
--

INSERT INTO `booking_orders` (`id`, `number`, `checkin`, `checkout`, `first_name`, `last_name`, `email`, `phone`) VALUES
(74, 0, '2020-06-27', '2020-06-28', '', '', '', ''),
(75, 0, '2020-06-27', '2020-06-29', '', '', '', ''),
(76, 0, '2020-06-27', '2020-07-01', '', '', '', ''),
(77, 0, '2020-06-28', '2020-06-29', '', '', '', '');

-- --------------------------------------------------------

--
-- Struktura tabulky `booking_order_room`
--

CREATE TABLE `booking_order_room` (
  `id` int(4) NOT NULL,
  `order_id` int(4) NOT NULL,
  `room_id` int(4) NOT NULL,
  `product_name` varchar(255) COLLATE utf8_czech_ci NOT NULL,
  `quantity` int(3) NOT NULL,
  `person` int(2) NOT NULL,
  `price` varchar(10) COLLATE utf8_czech_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci;

--
-- Vypisuji data pro tabulku `booking_order_room`
--

INSERT INTO `booking_order_room` (`id`, `order_id`, `room_id`, `product_name`, `quantity`, `person`, `price`) VALUES
(81, 74, 7, 'Standard', 1, 1, '50'),
(82, 75, 7, 'Standard', 1, 1, '50'),
(83, 76, 7, 'Standard', 1, 1, '50'),
(84, 77, 7, 'Standard', 1, 1, '50');

-- --------------------------------------------------------

--
-- Struktura tabulky `booking_rooms`
--

CREATE TABLE `booking_rooms` (
  `id` int(11) NOT NULL,
  `name` varchar(255) COLLATE utf8_czech_ci NOT NULL,
  `quantity` int(3) NOT NULL,
  `input` varchar(255) COLLATE utf8_czech_ci NOT NULL,
  `price` int(5) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci;

--
-- Vypisuji data pro tabulku `booking_rooms`
--

INSERT INTO `booking_rooms` (`id`, `name`, `quantity`, `input`, `price`) VALUES
(7, 'Standard', 2, 'standard1', 50),
(8, 'Deluxe', 4, 'deluxe2', 100);

--
-- Klíče pro exportované tabulky
--

--
-- Klíče pro tabulku `booking_orders`
--
ALTER TABLE `booking_orders`
  ADD PRIMARY KEY (`id`);

--
-- Klíče pro tabulku `booking_order_room`
--
ALTER TABLE `booking_order_room`
  ADD PRIMARY KEY (`id`);

--
-- Klíče pro tabulku `booking_rooms`
--
ALTER TABLE `booking_rooms`
  ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT pro tabulky
--

--
-- AUTO_INCREMENT pro tabulku `booking_orders`
--
ALTER TABLE `booking_orders`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=79;

--
-- AUTO_INCREMENT pro tabulku `booking_order_room`
--
ALTER TABLE `booking_order_room`
  MODIFY `id` int(4) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=85;

--
-- AUTO_INCREMENT pro tabulku `booking_rooms`
--
ALTER TABLE `booking_rooms`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=9;
COMMIT;

我附上我最后使用的解决方案。可能会有不同的做法

DROP TABLE IF EXISTS t1;

CREATE TEMPORARY TABLE t1 as (
SELECT r.name as name, r.id, sum(i.quantity) as qty
FROM booking_orders o
LEFT JOIN booking_order_room i on o.id = i.order_id
LEFT JOIN booking_rooms r on i.room_id = r.id
where
(checkin<'2020-07-17' and checkout>='2020-07-17') -- overlap at the end
OR (checkin<='2020-07-15' and checkout>'2020-07-15') -- overlap at the start
OR (checkin>='2020-07-15' and checkout<='2020-07-17') -- complete overlap
GROUP BY r.id
);

SELECT br.name as name, br.quantity as quantity, br.input as input, br. price as price, t1.qty 
FROM booking_rooms br
LEFT JOIN t1 ON t1.id = br.room_id
ORDER BY br.name DESC

期望的结果是什么样的?为什么不使用sql的between,而不是执行所有的日期检查。结果应该是:房间/号码可用,然后会有一个条件,如果他们是免费的,那么他们将去订购,如果不是这样的话unavailable@Tomm而BETWEEN是对OP所写内容的合理替代,在这种情况下,这是不合适的,因为它将无法解决当前的问题。
DROP TABLE IF EXISTS t1;

CREATE TEMPORARY TABLE t1 as (
SELECT r.name as name, r.id, sum(i.quantity) as qty
FROM booking_orders o
LEFT JOIN booking_order_room i on o.id = i.order_id
LEFT JOIN booking_rooms r on i.room_id = r.id
where
(checkin<'2020-07-17' and checkout>='2020-07-17') -- overlap at the end
OR (checkin<='2020-07-15' and checkout>'2020-07-15') -- overlap at the start
OR (checkin>='2020-07-15' and checkout<='2020-07-17') -- complete overlap
GROUP BY r.id
);

SELECT br.name as name, br.quantity as quantity, br.input as input, br. price as price, t1.qty 
FROM booking_rooms br
LEFT JOIN t1 ON t1.id = br.room_id
ORDER BY br.name DESC