具有语法错误的PostgreSQL子查询提供了有效的结果

时间:2021-05-08 15:42:19

What is happening here?

这里发生了什么?

I got two tables, test1 and test2:

我有两个表,test1和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:

PostgreSQL没有抱怨,执行查询并给我:

 id1
-----
(0 rows)

Is there any logic in this? Or should I file a bug report?

这有什么逻辑吗?或者我应该提交错误报告?

1 个解决方案

#1


3  

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);

#1


3  

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);