In SQL / MySQL, what is the difference between “ON” and “WHERE” in a join statement?

前端 未结 6 1945
臣服心动
臣服心动 2020-11-27 13:23

The following statements give the same result (one is using on, and the other using where):

mysql> select * from gifts INNER JOI         


        
6条回答
  •  孤街浪徒
    2020-11-27 13:55

    WHERE is a part of the SELECT query as a whole, ON is a part of each individual join.

    ON can only refer to the fields of previously used tables.

    When there is no actual match against a record in the left table, LEFT JOIN returns one record from the right table with all fields set to NULLS. WHERE clause then evaluates and filter this.

    In your query, only the records from gifts without match in 'sentgifts' are returned.

    Here's the example

    gifts
    
    1   Teddy bear
    2   Flowers
    
    sentgifts
    
    1   Alice
    1   Bob
    
    ---
    SELECT  *
    FROM    gifts g
    LEFT JOIN
            sentgifts sg
    ON      g.giftID = sg.giftID
    
    ---
    
    1  Teddy bear   1     Alice
    1  Teddy bear   1     Bob
    2  Flowers      NULL  NULL    -- no match in sentgifts
    
    ---
    SELECT  *
    FROM    gifts g
    LEFT JOIN
            sentgifts sg
    ON      g.giftID = sg.giftID
    WHERE   sg.giftID IS NULL
    
    ---
    
    2  Flowers      NULL  NULL    -- no match in sentgifts
    

    As you can see, no actual match can leave a NULL in sentgifts.id, so only the gifts that had not ever been sent are returned.

提交回复
热议问题