### INNER和LEFT OUTER加入帮助

``````Set 1 -> TableA INNER JOIN TableB
Set 2 -> TableC INNER JOIN TableB
``````

``````SELECT *
FROM TableA
INNER JOIN TableB ON ...
LEFT OUTER JOIN (TableC INNER JOIN TableB)
``````

#### 9 个答案:

`````` SELECT * FROM TableA
INNER JOIN TableB ON TableB.id = TableA.id
LEFT JOIN TABLEC ON TABLEC.id = TABLEB.id
``````

Rows-＆GT;您将从tableA的所有行开始。接下来，您将从TableA中删除表B中没有匹配“id”的所有行。（如果TableA和TableB之间不是1：1的关系，则会有重复项）。

``````SELECT * FROM TableA INNER JOIN TableB ON ...
UNION
SELECT * FROM TableC INNER JOIN TableD ON ...
``````

``````SELECT
FROM
(TableA INNER JOIN Table B ON <join criteria for A to B>)
LEFT OUTER JOIN
(TableC INNER JOIN Table D ON <join criteria for C to D>) ON
<join criteria for AxB to CxD>
``````

``````Select * from ((((TableA a inner join TableB b on a.id = b.id)
left outer join TableC c on b.id = c.id)
full outer join TableD d on c.id = d.id)
right outer join TableE e on e.id = d.id)
/* etc, etc... */
``````

``````SELECT *
FROM TableA              a
INNER JOIN TableB   b ON a.id=b.id
LEFT OUTER JOIN (SELECT *
FROM TableC            c
INNER JOIN TableD  d on c.id=d.id
) dt on b.id=dt.id
``````

``````SELECT *
FROM TableA A
INNER JOIN TableB B  ON B.?? = A.??  AND ...
LEFT JOIN TableC C  ON C.?? = B.??  AND ...
LEFT JOIN TableB B2 ON B2.?? = C.?? AND ...
LEFT JOIN TableD D  ON D.?? = C.??  AND ...
``````

``````SELECT *
FROM TableA INNER JOIN TableB on TableA.JoinColumn = TableB.JoinColumn
LEFT OUTER JOIN TableC on TableB.JoinColum = TableC.JoinColumn
INNER JOIN TableD on TableC.JoinColumn = TableD.JoinColumn
``````

``````SELECT a.a_id, b1.b_id b1_id, b2_id, bc.c_id
FROM TableA a JOIN TableB b1 on a.b_id = b1.b_id
LEFT JOIN (SELECT c.c_id, b2.b_id b2_id
FROM TableC c JOIN TableB b2 ON c.b_id = b2.b_id
) bc ON bc.c_id = a.c_id;
``````

``````SELECT <columns>
FROM (SELECT <columns> FROM TableA JOIN TableB ON <A-B join conditions>)
LEFT JOIN
(SELECT <columns> FROM TableC JOIN TableD ON <C-D join conditions>)
ON <AB-CD join conditions>
``````

``````SELECT <columns>
FROM (TableA a JOIN TableB b ON <A-B join conditions>)
LEFT JOIN
(TableC c JOIN TableD d ON <C-D join conditions>)
ON <AB-CD join conditions>
``````

AB-CD连接条件是直接用a，b，c，d等列写的。

``````create view AandB
as
select *
from A
inner join B on B.aid = A.aid
``````

``````select *
from AndB
left outer join CandD on AndB.cid = CandD.cid
``````

``````select *
from AndB
full outer join CandD on AndB.cid = CandD.cid
``````