SQL Different between Left join on… and Left Join on..where

后端 未结 1 608
别那么骄傲
别那么骄傲 2020-12-22 13:26

I have two sql to join two table together:

select top 100 a.XXX
              ,a.YYY
              ,a.ZZZ
              ,b.GGG
              ,b.JJJ
from tabl         


        
相关标签:
1条回答
  • 2020-12-22 13:50

    Whenever you are using LEFT JOIN, all the conditions about the content of the right table should be in the ON clause, otherwise you are effectively converting your LEFT JOIN to an INNER JOIN.

    The reason for that is that when a LEFT JOIN is used, all the rows from the left table will be returned. If they are matched by the right table, the values of the matching row(s) will be returned as well, but if they are not matched with any row on the right table, then the right table will return a row of null values.
    Since you can't compare anything to NULL (not even another NULL) (Read this answer to find out why), you are basically telling your database to return all rows that are matched in both tables.
    However, when the condition is in the ON clause, Your database knows to treat it as a part of the join condition.

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