【问题标题】:SQL semantic equivalence: {full, left, right} join on true = inner join on true?SQL 语义等价性:{full, left, right} join on true = inner join on true?
【发布时间】:2018-11-13 06:36:24
【问题描述】:

这两个 SQL 语句是否等效?

  1. select * from tb1 {full, left, right} join tb2 on true
  2. select * from tb1 inner join tb2 on true

在我看来,我认为这两种说法是等价的。但在 PostgreSQL 中,它不进行转换。我不知道其他数据库做什么。或者有没有我没有考虑过的情况?

【问题讨论】:

  • 根据this答案中提到的算法,当条件为真时,所有的连接都必须相等
  • “在 PostgreSQL 中,它不进行转换”是什么意思?
  • NATURAL JOIN behaves like JOIN ... ON TRUE, producing a cross-product join。所以它可能是交叉加入..当我读到Documentation
  • @S.Serpooshan 如果相等,在postgres的RBO阶段,应该改写为inner join。但是 postgres 不进行重写。
  • 我所说的相等,是他们用来查找结果的算法将产生相同的结果,而不是物理替换

标签: sql postgresql join


【解决方案1】:

这些构造不同相同:

select *
from tbl1 inner join
     tbl2
     on true;

和:

select *
from tbl1 left join
     tbl2
     on true;

在大多数情况下,两者都执行CROSS JOIN。但是,tbl2 没有行时会有所不同。

tbl2 没有行时,INNER JOIN 版本不返回任何行。 LEFT JOIN 版本返回第一个表中的行以及第二个表中的 NULL 值。

RIGHT JOINFULL JOIN 对空表有类似的行为。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-07
    • 1970-01-01
    • 2012-06-01
    • 2018-08-08
    • 2013-05-02
    相关资源
    最近更新 更多