在不收集正确行的日期之间进行查询

时间:2013-01-24 11:31:39

标签: sql oracle join oracle11g

在下面的查询中,我的原始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

现在我的查询似乎没有正确获取正确的行。此时的示例输出如下,以及当前在预订表中的内容,您可以看到返回了错误的行。

输出:

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...

任何人都可以帮我修改查询以便工作并包含正确的行吗?

1 个答案:

答案 0 :(得分:2)

我想你想要一个left outer join而不是where not exists

试试这个:

...
INNER JOIN features 
  ON home_feature.feature_id = features.feature_id
left outer join bookings b
  on homes.home_id = b.home_id
where (
        (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')
        )
      )
      or b.home_id is null
...

这将返回没有预订的房屋(or b.home_id is null)和预订符合您条件的房屋。

这应该归还所有四个家庭。

相关问题