1 回答
TA贡献1827条经验 获得超8个赞
附上我最终使用的解决方案。也许可以采取不同的做法。
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
- 1 回答
- 0 关注
- 102 浏览
添加回答
举报