sql - 如果连接表不返回结果,则不返回任何行
问题描述
我现在正在使用多个内部联接,但其中一个表可能根本不包含任何结果。
以下是我目前正在使用的查询
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_address
,等。street_address
property_history
解决方案
你应该使用LEFT OUTER JOIN
:
LEFT JOIN property_history ON property_history.property_id = property.id
推荐阅读
- node.js - Node.js 安装卡在一行代码
- go - 将数组附加到矩阵时的奇怪行为
- arrays - 从 Google 表格中最后一个空格后的单元格获取子字符串
- c# - Graphics.DrawString 方法不是从左上角绘制文本
- rust - Rust:在嵌套匹配表达式中可变地借用 self
- java - 如何防止仅针对某些领域的反射?
- mobile - 向 zbarcam 添加功能
- node.js - 部署时我无法让 nodejs 在 apache 上工作
- javascript - 检查每个 JSON 键中的特定数据
- apollo-server - 错误:您必须在调用 `server.applyMiddleware()` 之前`await server.start()`