如果联接表不返回结果,则不返回任何行

时间:2019-08-31 16:06:55

标签: sql postgresql

我现在正在使用多个内部联接,但是其中一个表可能根本不包含任何结果。

下面是我当前正在使用的查询

SELECT
   property.id,
   full_address,
   street_address,
   street.street,
   city.city as city,
   state.state_code as state_code,
   zipcode.zipcode as zipcode, 
   property_history.date_event AS event_date,
   property_history.event AS event
FROM
    property 
    INNER JOIN street  ON street.id = property.street_id 
    INNER JOIN city    ON city.id = property.city_id
    INNER JOIN state   ON state.id = property.state_id
    INNER JOIN zipcode ON zipcode.id = property.zipcode_id
    INNER JOIN property_history ON property_history.property_id = property.id
WHERE
    full_address = ?

现在,如果property_history不返回任何行,那么我什么也得不到。我认为我需要使用另一种类型的联接?

即使property.id为空,我仍然想获取full_addressstreet_addressproperty_history等。

1 个答案:

答案 0 :(得分:2)

您应该使用LEFT OUTER JOIN

LEFT JOIN property_history ON property_history.property_id = property.id