PostgreSQL subquery with syntax error gives a valid result

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-20 02:46:29

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!