SELECT DISTINCT r.roomId,rt.roomTypeName,rt.roomTypeDesc,rt.roomTypePrice
FROM Room r left join RoomType rt
on r.roomTypeId=rt.roomTypeId
WHERE roomStatus='Online'
AND NOT EXISTS(
SELECT 1 FROM Reservation b WHERE b.roomId = r.roomId
AND ('7/11/2015' BETWEEN b.checkInDate
AND b.checkOutDate OR '8/11/2015' BETWEEN b.checkInDate
AND b.checkOutDate OR ('7/11/2015'<= b.checkInDate AND '8/11/2015' >= b.checkOutDate)))
order by rt.roomTypePrice
我得到这个结果
如何计算每种房型的可用房数,以便可以使用[结果]中的下拉列表来允许用户选择所需数量? (以类似的名称登录Agoda.com。)
最佳答案
SELECT rt.roomTypeName, count(rt.roomTypeName)
FROM Room r left join RoomType rt
on r.roomTypeId=rt.roomTypeId
WHERE roomStatus='Online'
AND NOT EXISTS(
SELECT 1 FROM Reservation b WHERE b.roomId = r.roomId
AND ('7/11/2015' BETWEEN b.checkInDate
AND b.checkOutDate OR '8/11/2015' BETWEEN b.checkInDate
AND b.checkOutDate OR ('7/11/2015'<= b.checkInDate AND '8/11/2015' >= b.checkOutDate)))
GROUP BY rt.roomTypeName
有关
GROUP BY
的更多信息:http://www.w3schools.com/sql/sql_groupby.asp
这基本上是说“给我第1列中的每个
roomTypeName
以及第2列中roomTypeName不为null的所有记录的计数”这还不是唯一的,但是使用
GROUP BY
可以确保将结果集“汇总”为不同的roomTypeName
值,因此该计数然后应用于具有该roomTypeName
值的记录的出现次数。关于c# - SQL在ASP中的酒店房间可用性C#,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33827255/