问题
What is happening here?
I got two tables, test1 and test2:
create table test1 (id1 int4 primary key);
create table test2 (id2 int4 primary key);
As expected, this query:
select id1 from test2;
produces a syntax error:
ERROR: column "id1" does not exist
LINE 1: select id1 from test2;
However, when I try to execute this query:
select * from test1 where id1 in (select id1 from test2);
PostgreSQL doesn't complain, executes the query and gives me:
id1
-----
(0 rows)
Is there any logic in this? Or should I file a bug report?
回答1:
Columns from outer select
are visible in sub-select.
Your query is equivalent to:
select *
from test1
where test1.id1 in (select test1.id1 from test2);
来源:https://stackoverflow.com/questions/19407023/postgresql-subquery-with-syntax-error-gives-a-valid-result