[英]Join more than two tables
我正在嘗試加入 2 個表,在 1 個表中可能有也可能沒有相應的值。但我需要加入表並將字段列為空。
我嘗試將表作為左連接加入。但是如果第二個表中有兩個條目對應於第一個表的值,則第一個表數據將顯示兩次。
如果另一個表中有兩個數據或另一個表中沒有數據,我只需要顯示一次,但它應該顯示為空。
這是我的 sql 查詢。
SELECT *,incident.incident_id as incidentid
FROM register_incident AS incident
LEFT JOIN incident_images AS im ON im.incident_id=
incident.incident_id
WHERE incident.state='Active'
如果另一個表中沒有相應的行,我只需要顯示一次每個數據,但第二個表中的字段列表為空。
如果另一個表中有多於一行,則也將第一個表中的每一行顯示一次,第二個表中的一個條目。
您可以使用 select distinct 只獲取一行,例如(將 select 限制為 indicent_id ,但您可以添加您需要的 distinctcolumn ):
SELECT *,incident.incident_id as incidentid
FROM register_incident AS incident
LEFT JOIN (
select distinct incident_id from
incident_images
WHERE incident.state='Active' ) t ON t.incident_id= incident.incident_id
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.