[英]Select room availability between date (sql Query)
I have some problem with SQL query. 我对SQL查询有问题。 I'm looking for an available room between date from input form. 我正在寻找输入表单中日期之间的可用空间。
This is the table schema: 这是表模式:
Table Room 餐桌间
Table Price 价格表
id_price, id_room, price, date id_price,id_room,价格,日期
ex : 1, 1, 10, 2014-05-1 | 例如:1、1、10、2014-05-1 | 1, 1, 20, 2014-05-2 | 1,1,20,2014-05-2 | 1, 1, 10, 2014-05-3 1,1,10,2014-05-3
Table Booking 餐桌预订
What I'm looking for is: 我正在寻找的是:
If there are people booking vip room from 2014-5-1
to 2014-05-3
with room quantity is 8 如果有人在2014-5-1
至2014-05-3
预订VIP房间,房间数量为8
Then return blank (because vip room have 10 allotment and on 2014-05-1 are 3 booking, so just 7 room left) 然后返回空白(因为vip房间有10个分配空间,并且在2014-05-1上有3个预订,所以只剩下7个房间)
this is sql query 这是SQL查询
select room_name, COUNT( SELECT FROM booking WHERE date between '$checkin' and '$checkout') as total_booking, from room where allotment <= total_boking
Is this MySQL? 这是MySQL吗?
Syntax error; 语法错误; Also, date
is a reserved name. 此外, date
是保留名称。 See my revised tables and SQL below. 请参阅下面的修订表和SQL。
SELECT
room.room_name,
(SELECT COUNT(*) FROM booking WHERE booking.booking_date BETWEEN '' AND '') AS total_booking
FROM
room
WHERE room.allotment <= (SELECT COUNT(*) FROM booking WHERE booking.booking_date BETWEEN '' AND '')
Here is the table schema; 这是表模式;
-- ----------------------------
-- Table structure for `booking`
-- ----------------------------
DROP TABLE IF EXISTS `booking`;
CREATE TABLE `booking` (
`id_booking` int(10) NOT NULL,
`id_room` int(10) NOT NULL,
`booking_date` date NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
-- ----------------------------
-- Table structure for `price`
-- ----------------------------
DROP TABLE IF EXISTS `price`;
CREATE TABLE `price` (
`id_price` int(10) NOT NULL,
`id_room` int(10) NOT NULL,
`price` int(10) NOT NULL,
`price_date` date NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
-- ----------------------------
-- Table structure for `room`
-- ----------------------------
DROP TABLE IF EXISTS `room`;
CREATE TABLE `room` (
`id_room` int(10) NOT NULL,
`room_name` varchar(100) NOT NULL,
`allotment` int(10) NOT NULL,
PRIMARY KEY (`id_room`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
I would be inclined to write your query as: 我倾向于将您的查询写为:
select room_name,
(SELECT COUNT(*)
FROM booking b
WHERE b.id_room = r.id_room and date between '$checkin' and '$checkout'
) as total_booking
from room r
having allotment <= total_booking;
Note that the subquery is now correlated with the outer query, so total_booking
is for each room. 请注意,子查询现在与外部查询相关联,因此total_booking
适用于每个房间。
This also uses a MySQL extension where you can use column aliases defined in the select
clause for filtering by using the where
clause. 这也使用MySQL扩展,您可以在其中使用select
子句中定义的列别名来通过where
子句进行过滤。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.