sql - 具有语法错误的 PostgreSQL 子查询给出了有效结果

标签 sql postgresql subquery syntax-error

这里发生了什么?

我有两个表,test1 和 test2:

create table test1 (id1 int4 primary key);
create table test2 (id2 int4 primary key);

正如预期的那样,这个查询:

select id1 from test2;

产生语法错误:

ERROR:  column "id1" does not exist
LINE 1: select id1 from test2;

但是,当我尝试执行此查询时:

select * from test1 where id1 in (select id1 from test2);

PostgreSQL 没有提示,执行查询并给我:

 id1
-----
(0 rows)

这有逻辑吗?还是我应该提交错误报告?

最佳答案

来自外部 select 的列在子选择中可见。

您的查询等同于:

select * 
from test1 
where test1.id1 in (select test1.id1 from test2);

关于sql - 具有语法错误的 PostgreSQL 子查询给出了有效结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46215082/

相关文章:

php - MySQL SELECT 子查询多对多关系

mysql - 其他表中的 AVG 连接结果

Mysql触发器问题(我认为它正在触发)

sql - 为 Symfony2 正确加入 Doctrine2

python - 处理文件上传,使用 Pillow 调整大小,使用 SQLAlchemy 存储,使用 Flask 提供文件

PostgreSQL:如何识别数据类型?

postgresql - 使用 sequelize 切换 bool 列

mysql - SQL:本年度未购买过商品的客户

sql - 索引 View 更新时

sql - 如何更新 SQL 中游标获取的列