简体   繁体   English

内部联接和常规表的外部左联接

[英]Outer left join of inner join and regular table

For the first time in my professional career I found the need of join the result of a join with a table being the first join an inner join and the second a left outer join. 在我的职业生涯中,我第一次发现需要将联接结果与表连接起来,其中第一个联接是内部联接,第二个联接是左外部联接。

I came to this solution: 我来到这个解决方案:

SELECT * FROM
(
    TableA INNER JOIN TableB ON
    TableA.foreign = TableB.id
)
LEFT OUTER JOIN TableC
ON TableC.id = TableB.id

I know I could create a view for the first join but I wouldn't like to create new objects in my database. 我知道我可以为第一个联接创建一个视图,但是我不想在数据库中创建新对象。

Is this code right? 这个代码正确吗? I got coherent results but I would like to verify if it is theoretically correct. 我得到了一致的结果,但我想验证一下理论上是否正确。 Do you have any suggestion to improve this approach? 您对改进这种方法有什么建议吗?

You should do it in a simpler way. 您应该以更简单的方式进行操作。

SELECT * FROM
TableA
INNER JOIN TableB ON TableA.foreign = TableB.id
LEFT OUTER JOIN TableC ON TableB.id = TableC.id

Usually it is done that way. 通常是通过这种方式完成的。

I believe you are looking for something like this: 我相信您正在寻找这样的东西:

SELECT *, t.otherFields
FROM
(
    SELECT TableB.id, otherFields FROM TableA
    INNER JOIN TableB ON
    TableA.foreign = TableB.id
) t
LEFT OUTER JOIN TableC
ON TableC.id = t.id
SELECT * 
FROM TableA 
    INNER JOIN TableB ON
        TableA.foreign = TableB.id
LEFT OUTER JOIN TableC
    ON TableC.id = TableB.id

Also fine 还可以

Yes it is correct, but it does nothing different than the same (simpler) query without the parentheses: 是的,它是正确的,但它与没有括号的相同(简单)查询没有什么不同:

SELECT  * 
FROM    TableA 
        INNER JOIN TableB 
            ON TableA.foreign = TableB.id
        LEFT OUTER JOIN TableC
            ON TableC.id = TableB.id;

SQL Fiddle showing both queries with the same result SQL Fiddle显示两个具有相同结果的查询

Also worth noting that both queries have exactly the same execution plan 另外值得注意的是,两个查询的执行计划完全相同

The parentheses are used when you need to left join on the results of an inner join, eg If you wanted to return only results from table b where there was a record in tableC, but still left join to TableA you might write: 当您需要对内部联接的结果进行左联接时,会使用括号,例如,如果您只想返回表c中有记录的表b的结果,但仍然对表A保持联接,则可以这样写:

SELECT  * 
FROM    TableA 
        LEFT OUTER JOIN TableB 
            ON TableA.foreign = TableB.id
        INNER JOIN TableC
            ON TableC.id = TableB.id;

However this effectively turns the LEFT OUTER JOIN on TableB into an INNER JOIN because of the INNER JOIN below. 但是,由于下面的INNER JOIN,这实际上将TableB上的LEFT OUTER JOIN变成了INNER JOIN。 In this case you would use parentheses to alter the JOIN as required: 在这种情况下,您可以根据需要使用括号来更改JOIN:

SELECT  * 
FROM    TableA 
        LEFT OUTER JOIN (TableB 
        INNER JOIN TableC
            ON TableC.id = TableB.id)
            ON TableA.foreign = TableB.id;

SQL Fiddle to show difference in results when adding parentheses SQL小提琴在添加括号时显示结果差异

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM