![](/img/trans.png)
[英]SQL How to get FULL OUTER JOIN where primary key/field is a specific value in all tables
[英](SQL) Select and return all from more than 2 tables that have a common field where a specific value is present in all tables
如上所述,我希望結果是每個表中滿足該值的所有列的一行。 所有表中的公共字段是主鍵。 任何類型的UNION均無效,因為每個表中的列數都不相同。 我正在使用Access數據庫,因此不支持FULL OUTER JOINS。 我在C#,Visual Studio中的應用程序中使用此查詢,由於某種原因,如果沒有它給我語法錯誤,也無法進行內部聯接。 理想情況下,我希望在一個查詢中完成此操作。
例如,假設我有這些表:
表格1
userID name age
1 Bob 24
2 John 19
表2
userID col1 col2 col3
1 fd sd gh
...
表3
userID col4 col5 col6 col7
1 ff hg fd et
...
我想要結果:
userID name age col1 col2 col3 col4 col5 col6 col7 1 Bob 24 fd sd gh ff hg fd et
也許我最接近的是:
SELECT * FROM table1 AS b
INNER JOIN table2 AS c ON c.userID = b.userID
INNER JOIN table3 AS m ON m.userID = b.userID
WHERE userID = 1;
我得到的錯誤:
System.Data.dll中發生了類型為'System.Data.OleDb.OleDbException'的未處理異常
附加信息:查詢表達式'c.userID = b.userID INNER JOIN table3 AS m ON m.userID = b.UserI'中的語法錯誤(缺少運算符)。
我應該補充一點,就是我希望查詢具有適應性(假設我不知道這些列,但我知道它具有userID)
左聯接應該在ms-access中起作用:
select *
from Table1 t1
left join Table2 t2
on t1.userID = t2.userID
left join Table3 t3
on t1.userID = t3.userID
這應該在Acces中起作用:
Select T1.userID, T1.name, T1.age, T2.col1, T2.col2, T2.col3, T3.col4, T3.col5, T3.col6, T3.col7 from Table1 T1
left join Table2 T2 on T2.userID=T1.userID
left join Table3 T3 on T3.userID=T1.userID
或者,如果只希望第一個表中存在記錄,請使用內部聯接:
Select T1.userID, T1.name, T1.age, T2.col1, T2.col2, T2.col3, T3.col4, T3.col5, T3.col6, T3.col7 from Table1 T1
inner join Table2 T2 on T2.userID=T1.userID
inner join Table3 T3 on T3.userID=T1.userID
Select
t1.userID
,t1.name
,tb1.age
,tb2.col1
,tb2.col2
,tb2.col3
,tb3.col4
,tb3.col5
,tb3.col6
,tb3.col7
FROM table1 tb1
INNER JOIN table2 tb2 ON tb1.userID=tb2.userID
INNER JOIN table3 tb3 ON tb1.userID=tb3.userID
WHERE --some conditions if u desire.
所以最后我很親近。
在MS-Access中,當連接兩個以上的表時,必須使用括號(在第一個連接周圍?)。 第二,在我的where條件下,我沒有為userID指定表。 請參閱下面的修訂查詢:
SELECT * FROM table1 AS b
(INNER JOIN table2 AS c ON c.userID = b.userID)
INNER JOIN table3 AS m ON m.userID = b.userID
WHERE b.userID = 1;
我認為您的問題在condition.you沒有指定表的地方。
所以改變。
Where userId=1
成為
Where b.userId=1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.