开发者

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

Table1ID

Table 2

Table2ID

Subqueries

Table1ID

Table2ID

Is 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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜