繁体   English   中英

SQL联接的结果何时确切将两列合为一体?

[英]When exactly will the result of a SQL join collapse two columns into one?

我经常使用PostgreSQL,而且我发现有些相等联接将相等比较中涉及的两列合并为结果集中的单个列,有些则没有。 似乎与联接是否为INNER或OUTER有关,也与联接条件是使用USING语法还是ON语法表达有关。 我想知道两列何时准确折叠/合并为一个,甚至更好:我想知道在何处指定此行为。

using时它将折叠

create table a (id int);
create table b (id int);

select *
from
    a
    inner join
    b using (id);
 id 
----
(0 rows)

如果使用on ,它将返回两列:

select *
from
    a
    inner join
    b on a.id = b.id;
 id | id 
----+----
(0 rows)

而且没有表名限定的列是不明确的

select id
from
    a
    inner join
    b on a.id = b.id;
ERROR:  column reference "id" is ambiguous
LINE 1: select id from a inner join b on a.id = b.id;

outer join相同

select *
from
    a
    full outer join
    b on a.id = b.id;
 id | id 
----+----
(0 rows)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM