![](/img/trans.png)
[英]How to join result of two sql statements into one table and different columns
[英]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.