Why do CROSS JOIN conditions not work in the 'ON' clause, only the WHERE clause?

前端 未结 1 636
独厮守ぢ
独厮守ぢ 2021-01-03 09:42

I\'m wondering why a conditional cross join must have the condition(s) specified in the WHERE clause, and why it doesn\'t work in the \'ON\' clause. See link for com

相关标签:
1条回答
  • 2021-01-03 10:13

    CROSS JOIN is the SQL operator to perform a full cartesian product between two tables. Since it is a cartesian product, it does not allow any condition during the operation, you can only restrict its result with some filtering operation (the WHERE condition).

    JOIN (INNER and OUTER JOIN, that is) operators, are simply cartesian product together with the filtering operator expressed in the ON part of the operator (and in fact in the original syntax of SQL there was no JOIN operator, simply the “comma” notation to denote the product with the join condition expressed always in the WHERE part).

    Examples:

    "old" notation:

    SELECT ...
    FROM table1 t1, table2 t2
    WHERE t1.attribute = t2.attribute
    

    equivalent to the "modern" notation:

    SELECT ...
    FROM table1 t1 INNER JOIN table2 t2 ON t1.attribute = t2.attribute
    

    while, for the cartesian product:

    "old" notation:

    SELECT ...
    FROM table1 t1, table2 t2
    

    equivalent to the "modern" notation:

    SELECT ...
    FROM table1 t1 CROSS JOIN table2 t2
    

    In other words, a CROSS JOIN that require a condition is actually some kind of INNER JOIN.

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