• <bdo id='mbvWD'></bdo><ul id='mbvWD'></ul>

  • <i id='mbvWD'><tr id='mbvWD'><dt id='mbvWD'><q id='mbvWD'><span id='mbvWD'><b id='mbvWD'><form id='mbvWD'><ins id='mbvWD'></ins><ul id='mbvWD'></ul><sub id='mbvWD'></sub></form><legend id='mbvWD'></legend><bdo id='mbvWD'><pre id='mbvWD'><center id='mbvWD'></center></pre></bdo></b><th id='mbvWD'></th></span></q></dt></tr></i><div id='mbvWD'><tfoot id='mbvWD'></tfoot><dl id='mbvWD'><fieldset id='mbvWD'></fieldset></dl></div>
      <legend id='mbvWD'><style id='mbvWD'><dir id='mbvWD'><q id='mbvWD'></q></dir></style></legend>

        <tfoot id='mbvWD'></tfoot>

        <small id='mbvWD'></small><noframes id='mbvWD'>

        INNER JOIN 和 LEFT/RIGHT OUTER JOIN 的问题

        时间:2023-10-09

          <small id='TGgx6'></small><noframes id='TGgx6'>

            <tbody id='TGgx6'></tbody>
            <bdo id='TGgx6'></bdo><ul id='TGgx6'></ul>
          • <legend id='TGgx6'><style id='TGgx6'><dir id='TGgx6'><q id='TGgx6'></q></dir></style></legend>
                <i id='TGgx6'><tr id='TGgx6'><dt id='TGgx6'><q id='TGgx6'><span id='TGgx6'><b id='TGgx6'><form id='TGgx6'><ins id='TGgx6'></ins><ul id='TGgx6'></ul><sub id='TGgx6'></sub></form><legend id='TGgx6'></legend><bdo id='TGgx6'><pre id='TGgx6'><center id='TGgx6'></center></pre></bdo></b><th id='TGgx6'></th></span></q></dt></tr></i><div id='TGgx6'><tfoot id='TGgx6'></tfoot><dl id='TGgx6'><fieldset id='TGgx6'></fieldset></dl></div>

                1. <tfoot id='TGgx6'></tfoot>
                  本文介绍了INNER JOIN 和 LEFT/RIGHT OUTER JOIN 的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有三张桌子:

                  • 订单
                    • 订单 ID,整数 PK
                    • CustomerId, int FK to Customer, NULL 允许


                    • 客户
                      • 客户 ID,整数 PK
                      • CompanyId, int FK to Company, NULL 不允许


                      • 公司
                        • 公司 ID,整数 PK
                        • 名称,nvarchar(50)

                        我想选择所有订单,不管他们是否有客户,如果他们有客户,那么还有客户的公司名称.

                        I want to select all orders, no matter if they have a customer or not, and if they have a customer then also the customer's company name.

                        如果我使用这个查询...

                        If I use this query...

                        SELECT Orders.OrderId, Customers.CustomerId, Companies.Name
                        FROM   Orders
                               LEFT OUTER JOIN Customers
                                   ON Orders.CustomerId = Customers.CustomerId
                               INNER JOIN Companies
                                   OM Customers.CompanyId = Companies.CompanyId
                        

                        ...它只返回有客户的订单.如果我将 INNER JOIN 替换为 LEFT OUTER JOIN...

                        ...it only returns the orders that have a customer. If I replace INNER JOIN by LEFT OUTER JOIN...

                        SELECT Orders.OrderId, Customers.CustomerId, Companies.Name
                        FROM   Orders
                               LEFT OUTER JOIN Customers
                                   ON Orders.CustomerId = Customers.CustomerId
                               LEFT OUTER JOIN Companies
                                   OM Customers.CompanyId = Companies.CompanyId
                        

                        ...它有效,但我不明白为什么这是必要的,因为CustomersCompanies 之间的关系是必需的:客户必须em> 有一家公司.

                        ...it works but I don't understand why this is necessary because the relationship between Customers and Companies is required: A customer must have a company.

                        一种同样有效的替代方法似乎是:

                        An alternative approach which works as well seems to be:

                        SELECT Orders.OrderId, Customers.CustomerId, Companies.Name
                        FROM   Companies
                               INNER JOIN Customers
                                   ON Companies.CompanyId = Customers.CompanyId
                               RIGHT OUTER JOIN Orders
                                   OM Customers.CustomerId Orders.CustomerId
                        

                        此查询具有我期望的内连接和外连接的数量,但问题是我很难阅读,因为我将查询作为 orders 的查询是选择的根",而不是公司.另外RIGHT OUTER JOIN的用法我也比较陌生.

                        This query has the number of inner and outer joins that I expect but the problem is that it is hard to read for me because I have my query as a query of orders in mind where an order is the "root" of the selection and not the company. Also the usage of RIGHT OUTER JOIN is rather unfamiliar to me.

                        最后一个查询是 SQL Server Reporting Services 报表设计器生成的查询的一小部分.我正在尝试在没有设计器表面的情况下手动编写查询,因为它非常拥挤,并且在经过多次更改之后我在维护查询方面遇到了问题,并且预计将来会有更多更改.所以,我想以某种方式为查询提供一个可读的结构.

                        The last query is a small part of a query generated by the designer for SQL Server Reporting Services Reports. I am trying to write the query manually without the designer surface because it is very overcrowded and I'm having problems to maintain the query after many changes and more changes are expected in the future. So, I want to give the query a readable structure somehow.

                        问题:

                        1. 为什么查询 1 没有按我的预期工作?
                        2. 查询 2 是否是正确的解决方案,尽管(或因为?)它使用了两个 LEFT OTHER JOINS?
                        3. 查询 3 是正确的解决方案吗?
                        4. 有没有更好的方法来编写查询?
                        5. 是否有一些通用的经验法则和实践,如何以良好的可读方式编写具有大量外连接和内连接的查询?

                        推荐答案

                        从语义上讲,连接按照它们在 from 子句中出现的顺序进行处理.(由于 SQL 优化,它们实际上可能不会按此顺序执行,但顺序对于定义结果集很重要.)

                        Semantically, joins are processed in the order they appear in the from clause. (They may not be actually executed in this order due to SQL optimizations, but the ordering is important for defining the result set.)

                        所以,当你这样做时:

                        from orders left outer join customers inner join companies
                        

                        (我省略了 on 子句,它们会分散注意力.)

                        (I'm leaving out the on clauses which are a distraction for this purpose.)

                        SQL 解释为:

                        from (orders left outer join customers) inner join companies
                        

                        您正在执行内连接,因此值必须出现在两侧.在您的情况下,这会取消 左外连接 的效果.

                        You are doing an inner join, so the values must appear on both sides. In your case, this undoes the effect of the left outer join.

                        你想要:

                        from orders left outer join (customers inner join companies)
                        

                        这里有一些解决方案.

                        我的首选解决方案是对所有连接使用左外连接.事实上,为了可读性和可维护性,我编写的几乎每个查询都将只是left outer join[inner] join 连接表.如果您能以一致的形式编写查询,则必须解析查询以了解连接的语义似乎是不必要的工作.

                        My preferred solution is to use left outer join for all the joins. In fact, for readability and maintainability, almost every query I write is going to be only left outer join or [inner] join connecting the tables. Having to parse through the query to understand the semantics of the joins seems to be an unnecessary effort, if you can write the queries in a consistent form.

                        另一种解决方案是使用括号:

                        Another solution is to use parentheses:

                        from orders left outer join (customers inner join companies)
                        

                        另一种解决方案是子查询:

                        Another solution is a subquery:

                        from orders left outer join (select . . . from customers inner join companies) cc
                        

                        这篇关于INNER JOIN 和 LEFT/RIGHT OUTER JOIN 的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  <i id='w6fNX'><tr id='w6fNX'><dt id='w6fNX'><q id='w6fNX'><span id='w6fNX'><b id='w6fNX'><form id='w6fNX'><ins id='w6fNX'></ins><ul id='w6fNX'></ul><sub id='w6fNX'></sub></form><legend id='w6fNX'></legend><bdo id='w6fNX'><pre id='w6fNX'><center id='w6fNX'></center></pre></bdo></b><th id='w6fNX'></th></span></q></dt></tr></i><div id='w6fNX'><tfoot id='w6fNX'></tfoot><dl id='w6fNX'><fieldset id='w6fNX'></fieldset></dl></div>

                      • <bdo id='w6fNX'></bdo><ul id='w6fNX'></ul>
                      • <legend id='w6fNX'><style id='w6fNX'><dir id='w6fNX'><q id='w6fNX'></q></dir></style></legend>
                      • <tfoot id='w6fNX'></tfoot>

                        <small id='w6fNX'></small><noframes id='w6fNX'>

                          <tbody id='w6fNX'></tbody>