选择日期之间的房间可用性(sql查询)

时间:2014-08-12 01:48:43

标签: php mysql sql

我的SQL查询有些问题。我正在寻找输入表单日期之间的可用空间。

这是表架构:

桌面房间

  • id_room,room_name,alootment
  • ex:1,vip,10

表价格

  • id_price,id_room,price,date

  • ex:1,1,10,2014-05-1 | 1,1,20,205-05-2 | 1,1,10,2014-05-3

餐桌预订

  • id_booking,id_room,date
  • ex:1,1,205-05-1 | 2,1,2014-05-1 | 3,1,2014-05-1

我正在寻找的是:

如果有人预订从2014-5-12014-05-3的贵宾室,房间数量为8

然后返回空白(因为贵宾室有10个分配,2014-05-1是3个预订,所以只剩下7个房间)

这是sql查询

select room_name, COUNT( SELECT FROM booking WHERE date between '$checkin' and '$checkout') as total_booking, from room where allotment <= total_boking

2 个答案:

答案 0 :(得分:0)

这是MySQL吗?

语法错误;此外,date是保留名称。请参阅下面的修订表和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 '')

这是表架构;

-- ----------------------------
-- 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;

答案 1 :(得分:0)

我倾向于将您的查询写成:

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;

请注意,子查询现在与外部查询相关联,因此total_booking适用于每个房间。

这也使用MySQL扩展,您可以使用select子句中定义的列别名通过where子句进行过滤。