我构建了一个SQL查询来获取所有可用于出租且未预订的 select property.address
from (period join booking on period.propertyid = booking.propertyid) join property on period.propertyid = property.id
where period.type = 'available'
AND period.end >= TO_DATE('2016-01-25', 'YYYY-MM-DD')
AND period.start <= TO_DATE('2016-01-20', 'YYYY-MM-DD')
AND (
booking.start > TO_DATE('2016-01-25', 'YYYY-MM-DD') OR
booking.start < TO_DATE('2016-01-20', 'YYYY-MM-DD')
)
AND (
booking.end > TO_DATE('2016-01-25', 'YYYY-MM-DD') OR
booking.end < TO_DATE('2016-01-20', 'YYYY-MM-DD')
);
地址:
period join booking on period.propertyid = booking.propertyid
但由于periods
,它不会查询properties
表中不存在的booking
(公寓)的booking
。
如何加入periods
表,以便仍然查询所有{{1}}?
答案 0 :(得分:1)
使用LEFT JOIN
:
select property.address
from (
period
left join booking on period.propertyid = booking.propertyid)
...
答案 1 :(得分:0)
SELECT property.address
FROM property
LEFT JOIN period ON period.propertyid = property.id
LEFT JOIN booking ON period.propertyid = booking.propertyid
WHERE period.type = 'available'
AND period.end >= '2016-01-25'
AND period.start <= '2016-01-20'
AND (booking.start > '2016-01-25' OR booking.start < '2016-01-20')
AND (booking.end > '2016-01-25' OR booking.end < '2016-01-20')