SQL JOIN (SELECT ... ) ue ON 1=1?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35374860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 04:25:41  来源:igfitidea点击:

JOIN (SELECT ... ) ue ON 1=1?

sqlpostgresqljoinleft-joinamazon-redshift

提问by kee

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

我正在 Redshift 中阅读 SQL 查询,但无法理解最后一部分:

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

What does ON 1=1mean here?

ON 1=1这里是什么意思?

采纳答案by Buddy Yaussy

It's simply doing a cross join, which selects all rows from the first table and all rows from the second table and shows as cartesian product, i.e. with all possibilities.

它只是做一个交叉连接,它选择第一个表中的所有行和第二个表中的所有行,并显示为笛卡尔积,即具有所有可能性。

JOIN (LEFT, INNER, RIGHT, etc.) statements normally require an 'ON ..." condition. Putting in 1=1 is like saying "1=1 is always true, do don't eliminate anything".

JOIN (LEFT, INNER, RIGHT, etc.) 语句通常需要一个“ON ...”条件。放入 1=1 就像说“1=1 总是正确的,不要消除任何东西”。

回答by Erwin Brandstetter

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

意图是无条件的LEFT JOIN,它与 a 的不同之CROSS JOIN在于返回左表表达式中的所有行,即使右表表达式中没有匹配项 - 而 aCROSS JOIN从结果中删除这些行。更多关于加入手册。

However:

然而:

1=1is pointlessin Postgres and all derivativesincluding Amazon Redshift. Just use true. This has probably been carried over from another RDBMS that does not support the booleantype properly.

1=1在 Postgres 和包括 Amazon Redshift 在内的所有衍生产品中毫无意义。只需使用true. 这可能是从另一个不boolean正确支持该类型的RDBMS 继承而来的。

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

Then again, LEFT JOINis pointless for this particular subquery with SELECT MIN(modified) FROM useron the right, because a SELECTwith an aggregate function (min()) and no GROUP BYclause always returns exactly one row. This case (but not other cases where no rowmight be found) can be simplified to:

再说一次,LEFT JOIN对于SELECT MIN(modified) FROM user右边的这个特定子查询是没有意义的,因为SELECT带有聚合函数 ( min()) 并且没有GROUP BY子句的 a 总是只返回一行。这种情况(但不是其他可能找不到行的情况)可以简化为:

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

回答by data_set

I believe its used to emulate cartesian join.

我相信它用于模拟笛卡尔连接。

From your query, the least modified value (It will be just 1 element) will be assigned to all the records of the left table.

从您的查询中,最少修改的值(它将是 1 个元素)将分配给左表的所有记录。

PS: Left join is not much useful here. Might as well just use inner join

PS:左连接在这里用处不大。不妨只使用内连接