SQL JOIN - WHERE clause vs. ON clause

前端 未结 19 1598
深忆病人
深忆病人 2020-11-21 11:56

After reading it, this is not a duplicate of Explicit vs Implicit SQL Joins. The answer may be related (or even the same) but the question is diffe

相关标签:
19条回答
  • 2020-11-21 12:56

    Let's consider those tables :

    A

    id | SomeData
    

    B

    id | id_A | SomeOtherData
    

    id_A being a foreign key to table A

    Writting this query :

    SELECT *
    FROM A
    LEFT JOIN B
    ON A.id = B.id_A;
    

    Will provide this result :

    / : part of the result
                                           B
                          +---------------------------------+
                A         |                                 |
    +---------------------+-------+                         |
    |/////////////////////|///////|                         |
    |/////////////////////|///////|                         |
    |/////////////////////|///////|                         |
    |/////////////////////|///////|                         |
    |/////////////////////+-------+-------------------------+
    |/////////////////////////////|
    +-----------------------------+
    

    What is in A but not in B means that there is null values for B.


    Now, let's consider a specific part in B.id_A, and highlight it from the previous result :

    / : part of the result
    * : part of the result with the specific B.id_A
                                           B
                          +---------------------------------+
                A         |                                 |
    +---------------------+-------+                         |
    |/////////////////////|///////|                         |
    |/////////////////////|///////|                         |
    |/////////////////////+---+///|                         |
    |/////////////////////|***|///|                         |
    |/////////////////////+---+---+-------------------------+
    |/////////////////////////////|
    +-----------------------------+
    

    Writting this query :

    SELECT *
    FROM A
    LEFT JOIN B
    ON A.id = B.id_A
    AND B.id_A = SpecificPart;
    

    Will provide this result :

    / : part of the result
    * : part of the result with the specific B.id_A
                                           B
                          +---------------------------------+
                A         |                                 |
    +---------------------+-------+                         |
    |/////////////////////|       |                         |
    |/////////////////////|       |                         |
    |/////////////////////+---+   |                         |
    |/////////////////////|***|   |                         |
    |/////////////////////+---+---+-------------------------+
    |/////////////////////////////|
    +-----------------------------+
    

    Because this removes in the inner join the values that aren't in B.id_A = SpecificPart


    Now, let's change the query to this :

    SELECT *
    FROM A
    LEFT JOIN B
    ON A.id = B.id_A
    WHERE B.id_A = SpecificPart;
    

    The result is now :

    / : part of the result
    * : part of the result with the specific B.id_A
                                           B
                          +---------------------------------+
                A         |                                 |
    +---------------------+-------+                         |
    |                     |       |                         |
    |                     |       |                         |
    |                     +---+   |                         |
    |                     |***|   |                         |
    |                     +---+---+-------------------------+
    |                             |
    +-----------------------------+
    

    Because the whole result is filtered against B.id_A = SpecificPart removing the parts B.id_A IS NULL, that are in the A that aren't in B

    0 讨论(0)
提交回复
热议问题