Join Unlike Tables
I have 2 unlike tables and a large set of subqueries that have a key for each of those two tables. I need to join the two tables to each subquery.
Table 1
Table1IDTable 2
Table2IDSubqueries
Table1ID Table2IDIs there any way to join everything together?
I have tried something similar to
SELECT Table1.Table1ID, Table2.Table2ID
FROM Table1, Table2
LEFT JOIN (SELECT Table开发者_开发问答1ID, Table2ID FROM ....) q1 ON Table1.Table1ID = q1.Table1ID AND Table2.Table2ID = q1.Table2ID
...
This following query will select all fields from a join of all three tables on the respective table IDs:
SELECT *
FROM Table1 t1
INNER JOIN Subqueries s
ON t1.Tabl1Id = s.Table1Id
INNER JOIN Table2 t2
ON s.Tabl2Id = ts.Table2Id
If you need absolutely all records from both Table1 and Table2, whether they are joined via the Subqueries table, then you can change the join to FULL OUTER:
SELECT *
FROM Table1 t1
FULL OUTER JOIN Subqueries s
ON t1.Tabl1Id = s.Table1Id
FULL OUTER JOIN Table2 t2
ON s.Tabl2Id = ts.Table2Id
精彩评论