在下面的查询中,我的原始 ERD 中有 5 个表。该查询旨在从 Homes 表中选择房屋,并从相应的表中选择其特征和类型。我遇到的问题是在输入查询的特定日期范围之间选择预订表中不存在的房屋。
Bookings >-- Home >-- Home_Type
|
^
Home_Feature
V
|
Feature
SELECT homes.home_id,
homes.title,
homes.description,
homes.living_room_count,
homes.bedroom_count,
homes.bathroom_count,
homes.price, homes.sqft,
listagg(features.feature_name, '\n') WITHIN GROUP(ORDER BY features.feature_name) features,
home_type.type_name
FROM homes
INNER JOIN home_feature
ON homes.home_id = home_feature.home_id
INNER JOIN home_type
ON home_type.type_code = homes.type_code
INNER JOIN features
ON home_feature.feature_id = features.feature_id
WHERE NOT EXISTS (SELECT home_id
FROM bookings b
WHERE b.home_id = homes.home_id
AND (b.booking_end <= to_date('25-Jan-13') OR b.booking_end >= to_date('21-Jan-13'))
AND (b.booking_start <= to_date('25-Jan-13') OR b.booking_start >= to_date('21-Jan-13')))
GROUP BY homes.home_id, homes.title,
homes.description, homes.living_room_count,
homes.bedroom_count, homes.bathroom_count,
homes.price, homes.sqft, home_type.type_name
现在,我的查询似乎没有正确获得正确的行。下面是一个示例输出以及当前在 bookings 表中的内容,您可以看到返回的行不正确。
输出:
Home_ID Title Description LivingRooms Bedrooms Bathrooms Price SQFT Type Features
3 Home A A House 2 2 2 200 500 Flat TV...
4 Home B B House 3 1 1 250 600 House Pool...
预订表:
Home_ID Booking_ID Customer_ID Booking_Start Booking_End
1 1 1 22-Jan-13 23-Jan-13
2 2 3 27-Jan-13 29-Jan-13
家园表:
Home_ID ....
1 ....
2
3
4
显然,此时查询的输出也应该包括 Home_ID 2 但不是,但我的印象是查询应该可以工作?
目前的查询应该是这样的:
Home_ID Title Description LivingRooms Bedrooms Bathrooms Price SQFT Type Features
2 Home 2 2 House 3 1 1 100 300 Flat Balcony...
3 Home 3 3 House 2 2 2 200 500 Flat TV...
4 Home 4 4 House 3 1 1 250 600 House Pool...
谁能帮我修改查询以便工作并包含正确的行?