1. <small id='fIzjt'></small><noframes id='fIzjt'>

      <legend id='fIzjt'><style id='fIzjt'><dir id='fIzjt'><q id='fIzjt'></q></dir></style></legend>

        <bdo id='fIzjt'></bdo><ul id='fIzjt'></ul>
    2. <tfoot id='fIzjt'></tfoot>

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

      什么时候用LEFT JOIN,什么时候用INNER JOIN?

      时间:2023-10-09

      • <legend id='MnIFs'><style id='MnIFs'><dir id='MnIFs'><q id='MnIFs'></q></dir></style></legend>

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

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

                  <tbody id='MnIFs'></tbody>
                本文介绍了什么时候用LEFT JOIN,什么时候用INNER JOIN?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我觉得我总是被教导使用 LEFT JOIN 并且我经常看到它们与 INNER 混合使用以在几段代码中完成相同类型的查询应该在不同的页面上做同样的事情.这是:

                I feel like I was always taught to use LEFT JOINs and I often see them mixed with INNERs to accomplish the same type of query throughout several pieces of code that are supposed to do the same thing on different pages. Here goes:

                SELECT ac.reac, pt.pt_name, soc.soc_name, pt.pt_soc_code
                FROM
                  AECounts ac
                  INNER JOIN 1_low_level_term llt on ac.reac = llt.llt_name
                  LEFT JOIN 1_pref_term pt ON llt.pt_code = pt.pt_code
                  LEFT JOIN 1_soc_term soc ON pt.pt_soc_code = soc.soc_code
                LIMIT 100,10000
                

                这是我正在研究的一个:

                Thats one I am working on:

                我看到很多:

                SELECT COUNT(DISTINCT p.`case`) as count
                FROM FDA_CaseReports cr
                  INNER JOIN ae_indi i ON i.isr = cr.isr
                  LEFT JOIN ae_case_profile p ON cr.isr = p.isr
                

                这看起来像 LEFT 也可能是 INNER 有什么问题吗?

                This seems like the LEFT may as well be INNER is there any catch?

                推荐答案

                有什么问题吗?是的 - 左联接是外联接的一种形式,而内联接是内联接的一种形式.

                Is there any catch? Yes there is -- left joins are a form of outer join, while inner joins are a form of, well, inner join.

                以下是显示差异的示例.我们将从基础数据开始:

                Here's examples that show the difference. We'll start with the base data:

                mysql> select * from j1;
                +----+------------+
                | id | thing      |
                +----+------------+
                |  1 | hi         |
                |  2 | hello      |
                |  3 | guten tag  |
                |  4 | ciao       |
                |  5 | buongiorno |
                +----+------------+
                
                mysql> select * from j2;
                +----+-----------+
                | id | thing     |
                +----+-----------+
                |  1 | bye       |
                |  3 | tschau    |
                |  4 | au revoir |
                |  6 | so long   |
                |  7 | tschuessi |
                +----+-----------+
                

                这里我们将看到内连接和左连接之间的区别:

                And here we'll see the difference between an inner join and a left join:

                mysql> select * from j1 inner join j2 on j1.id = j2.id;
                +----+-----------+----+-----------+
                | id | thing     | id | thing     |
                +----+-----------+----+-----------+
                |  1 | hi        |  1 | bye       |
                |  3 | guten tag |  3 | tschau    |
                |  4 | ciao      |  4 | au revoir |
                +----+-----------+----+-----------+
                

                嗯,3 行.

                mysql> select * from j1 left join j2 on j1.id = j2.id;
                +----+------------+------+-----------+
                | id | thing      | id   | thing     |
                +----+------------+------+-----------+
                |  1 | hi         |    1 | bye       |
                |  2 | hello      | NULL | NULL      |
                |  3 | guten tag  |    3 | tschau    |
                |  4 | ciao       |    4 | au revoir |
                |  5 | buongiorno | NULL | NULL      |
                +----+------------+------+-----------+
                

                哇,5 行!发生了什么?

                Wow, 5 rows! What happened?

                外部连接,例如 left join 保留不匹配的行——因此左连接查询保留 id 为 2 和 5 的行.其余列用 NULL 填充.

                Outer joins such as left join preserve rows that don't match -- so rows with id 2 and 5 are preserved by the left join query. The remaining columns are filled in with NULL.

                换句话说,左连接和内连接不可互换.

                In other words, left and inner joins are not interchangeable.

                这篇关于什么时候用LEFT JOIN,什么时候用INNER JOIN?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:Mysql - 帮助我更改此搜索查询以获得所需的结果 下一篇:针对日期范围的 SQL 连接?

                相关文章

                <legend id='XvGZr'><style id='XvGZr'><dir id='XvGZr'><q id='XvGZr'></q></dir></style></legend>

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

                    <bdo id='XvGZr'></bdo><ul id='XvGZr'></ul>
                2. <small id='XvGZr'></small><noframes id='XvGZr'>

                  <tfoot id='XvGZr'></tfoot>