[英]Inner, outer or full join on three tables in MySQL database on linux with apache
[英]mySQL full outer join with a inner join?
我不確定解決此問題的最佳方法,即使這是可能的。 假設我有4個表,類別,文檔,other_documents,more_documents,只有文檔具有其他類別沒有的類別。 我正在使用MySQL和PHP。 我知道完全外部聯接不是mysql中的選項,必須使用左右聯接來解決。
我可以獲取所有文檔,並在其中加入ID匹配的類別,太好了!
現在,我有了上述數據集,我還希望將other_documents和more_documents“添加”到上述結果集的底部,並在類別未排列的位置填充空值。
說在more_documents表中,有一個唯一列,而document和other_documents表則沒有。 例如,phil和dave將數據提交到document和other_documents表中,而jon僅將數據提交到more_documents表中,因此結果將顯示為:
ID-標題-發布者-類別-唯一的more_documents列
1-doc1-dave-全局-空
2-doc2-phil-小時-空
3-doc3-dave-操作-null
4-doc4-dave-全局-null
5-Doc5-喬恩-全球-12345
6-doc6-喬恩-小時-12345
您要使用“驅動程序”子查詢來構造查詢:
select driver.Id, driver.Title, driver.publisher, c.category
from ((select id, categoryId, Title, publisher
from documents
) union
(select id, NULL, Title, publisher
from more_documents
) union
(select id, NULL, Title, publisher
from other_documents
)
) driver left outer join
category c
on driver.category_id = c.categoryId
請注意,在driver
子查詢中使用union
,因為它會刪除重復項。
您可能需要返回到文檔表的其他聯接,才能拾取僅在一個表中的字段。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.