JOIN (SELECT … ) ue ON 1=1?

前端 未结 4 2064
野的像风
野的像风 2020-12-15 04:59

I am reading an SQL query in Redshift and can\'t understand the last part:

...
LEFT JOIN (SELECT MIN(modified) AS first_modified FROM user) ue
ON 1=1
         


        
4条回答
  •  死守一世寂寞
    2020-12-15 05:51

    The intention is an unconditional LEFT JOIN, which is different from a CROSS JOIN in that all rows from the left table expression are returned, even if there is no match in the right table expression - while a CROSS JOIN drops such rows from the result. More on joins in the manual.

    However:

    1=1 is pointless in Postgres and all derivatives including Amazon Redshift. Just use true. This has probably been carried over from another RDBMS that does not support the boolean type properly.

    ... LEFT JOIN (SELECT  ...) ue ON true
    

    Then again, LEFT JOIN is pointless for this particular subquery with SELECT MIN(modified) FROM user on the right, because a SELECT with an aggregate function (min()) and no GROUP BY clause always returns exactly one row. This case (but not other cases where no row might be found) can be simplified to:

    ... CROSS JOIN (SELECT MIN(modified) AS first_modified FROM user) ue
    

提交回复
热议问题