Conditions in LEFT JOIN (OUTER JOIN) vs INNER JOIN

前端 未结 3 743
一向
一向 2020-12-11 11:15
SELECT A.COL1, B.COL1,C.COL1
FROM TABLEA A
LEFT JOIN TABLEB B ON A.COL1 = B.COL1
LEFT JOIN TABLEC C ON (
        C.COL3 IS NOT NULL
        AND (
              C.COL         


        
3条回答
  •  时光取名叫无心
    2020-12-11 11:28

    Earlier versions of ANSI SQL did not contain the ON clause for join conditions – it used the where clause for everything. This was fine for inner joins, but as database applications started using outer joins, there were problems that arose with this approach. Some of you may remember the original ANSI-89-era syntax for *= and =*. These were used on the predicates to define the behavior for an outer join. In this case, we’ll preserve non-matching rows from A in addition to the normal rows returned from a join:

    SELECT A.COL1, B.COL1,C.COL1
    FROM TABLEA A
    LEFT JOIN TABLEB B ON A.COL1 = B.COL1
    LEFT JOIN TABLEC C ON (
            C.COL3 IS NOT NULL
            AND (
                  C.COL2 = 664
                  AND A.COL1 = C.COL1
            )
    )
    

    This is very helpful when you are trying to figure out what should happen in a query. It helps semantically define the set of rows that should return from the join.

    The ON clause: This syntax allows you to specify the column names for join keys in both tables The ON clause is used to join tables where the column names don’t match in both tables. The join conditions are removed from the filter conditions in the WHERE clause.

提交回复
热议问题