假设有两个表:
Table A: A1, A2, A_Other
Table B: B1, B2, B_Other
在以下示例中, is something
是针对固定值(例如 = 'ABC'
或 < 45
)检查的条件。
我写了如下查询(1):
Select * from A
Where A1 IN (
Select Distinct B1 from B
Where B2 is something
And A2 is something
);
我真正想写的是(2):
Select * from A
Where A1 IN (
Select Distinct B1 from B
Where B2 is something
)
And A2 is something;
奇怪的是,两个查询都返回了相同的结果。查看查询1的解释计划时,它看起来像执行子查询时,因为条件不适用于子查询,所以它被推迟用作主查询结果的过滤器。A2 is something
我通常希望查询1失败,因为子查询本身会失败:
Select Distinct B1 from B
Where B2 is something
And A2 is something; --- ERROR: column "A2" does not exist
但我发现情况并非如此,Postgres 将不适用的子查询条件推迟到主查询。
这是标准行为还是 Postgres 异常?这是在哪里记录的,这个功能叫什么?
另外,我发现如果我A2
在 table 中添加一列B
,则只有查询2可以按预期工作。在这种情况下A2
,查询2中的引用仍将引用A.A2
,但查询1中的引用将引用新列B.A2
,因为它现在可以直接应用于子查询。