用到的表有四张,关键字段如下:
酒店表
房间类型表
房间表
房间订单表,订单表会有逻辑限制,使得开始时间在开始日的下午2点以后,或者与结束日同一天的九点之前,结束时间统一在结束日中午12点
查询SQL思路如下
#第一步从订单表中查出结束时间在当前天结束点之后的订单,即可查出那些房间还在有客状态
select ro.roomId from room_order ro where ro.orderEndTime > concat(curdate(),' 12:00:00');
#第二步连接房间表查出那些房间没有订单,既那些房间可以接客
select
ro.roomId,rm.id,rm.roomName
from (
select roomId from room_order where orderEndTime > concat(curdate(),' 12:00:00')
) ro right join room rm on ro.roomId=rm.id where ro.roomId is null
#第三步可接客房间连接房间类型表和酒店表查出供客户下单用的房间类型列表
select
ho.hotelName,rc.roomClassName,count(rm.roomClassId) '剩余房间数'
from (
select
rm.id,rm.roomClassId,rm.roomName
from (
select roomId from room_order where orderEndTime > concat(curdate(),' 12:00:00')
) ro right join room rm on ro.roomId=rm.id where ro.roomId is null
) rm right join room_class rc on rm.roomClassId=rc.id
right join hotel ho on rc.hotelId=ho.id
group by rm.roomClassId
#第四步优化SQL查询出全部房间类型对应的房间数
select
ho.hotelName,rc.roomClassName,SUM(rm.cou) '剩余房间数'
from (
select
case when ro.roomId is NULL then 1 when ro.roomId is not NULL then 0 end cou,rm.roomClassId
from (
select roomId from room_order where orderEndTime > concat(curdate(),' 12:00:00')
) ro right join room rm on ro.roomId=rm.id
) rm right join room_class rc on rm.roomClassId=rc.id
right join hotel ho on rc.hotelId=ho.id
group by rm.roomClassId
#第五步 查询出有客和无客的房间是哪些
select
case when ro.roomId is NULL then '无客' when ro.roomId is not NULL then '有客' end mes,ro.roomId,rm.id,rm.roomClassId,rm.roomName
from (
select roomId from room_order where orderEndTime > concat(curdate(),' 12:00:00')
) ro right join room rm on ro.roomId=rm.id