使用using
そしてちょうどpj.name
あるいは単にname
create table person (id serial, name text);
insert into person (name) values ('John'),('Mary');
select *
from (
select *
from
person p1
inner join
person p2 using(name)
) r
where name = 'John'
;
name | id | id
------+----+----
John | 1 | 1
A clause of the form USING (a, b, ...) is shorthand for ON left_table.a = right_table.a AND left_table.b = right_table.b .... Also, USING implies that only one of each pair of equivalent columns will be included in the join output, not both.
join
の片側のみが必要な場合:
select *
from (
select p1.*
from
person p1
inner join
person p2 using(name)
) r
where name = 'John'
;
id | name
----+------
1 | John
両方join
サイドがその後、レコードを必要に応じ使用している場合:
select (p2).id, (p1).name -- or (p2).*, (p1).*
from (
select p1, p2
from
person p1
inner join
person p2 using(name)
) r
where (p1).name = 'John'
;
id | name
----+------
1 | John
残念ながら、 'ERROR:column reference"の名前は "あいまいです"。 –
@PhilipKamenarsky更新された回答を確認してください –
それだけです!レコードについて知りませんでした、それはそれを解決します。 –