为什么postgresql认为这个子查询列名不完全合格和含糊不清

时间:2021-07-15 08:02:47
  Select distinct A.col1, B.col2, col3
   from A inner join B on A.id = B.id
   and B.id in 

       (select distinct col2 from B where ..... )

PostgreSQL's plpgsql parser does not like the unqualified "col2" in the parenthetic subquery. Since there's only one table mentioned in the subquery, isn't the parser manufacturing an ambiguity where there is none?

PostgreSQL的plpgsql解析器不喜欢括号子查询中的不合格“col2”。既然子查询中只提到了一个表,那么解析器是否会产生不明确的歧义?

2 个解决方案

#1


1  

An SQL query like you display would just work.

像你显示的SQL查询就可以了。

Since you mention plpgsql, my shot in the dark is that you are only showing fragments from what is actually a plpgsql function and you run into naming conflicts with function parameters, which are visible anywhere in the function body (except dynamic SQL).

既然你提到了plpgsql,那么我在黑暗中的镜头就是你只显示了实际上是plpgsql函数的片段,并且你遇到了函数参数的命名冲突,这些参数在函数体的任何地方都可见(动态SQL除外)。

#2


2  

No. Outer query columns are available in the subquery too, otherwise you couldn't bind the inner and outer queries together. It's an easy thing to forget and can catch you out if you're not expecting it.

不可以。外部查询列也可以在子查询中使用,否则您无法将内部和外部查询绑定在一起。这是一件容易忘记的事情,如果你没想到的话可以把你赶出去。

#1


1  

An SQL query like you display would just work.

像你显示的SQL查询就可以了。

Since you mention plpgsql, my shot in the dark is that you are only showing fragments from what is actually a plpgsql function and you run into naming conflicts with function parameters, which are visible anywhere in the function body (except dynamic SQL).

既然你提到了plpgsql,那么我在黑暗中的镜头就是你只显示了实际上是plpgsql函数的片段,并且你遇到了函数参数的命名冲突,这些参数在函数体的任何地方都可见(动态SQL除外)。

#2


2  

No. Outer query columns are available in the subquery too, otherwise you couldn't bind the inner and outer queries together. It's an easy thing to forget and can catch you out if you're not expecting it.

不可以。外部查询列也可以在子查询中使用,否则您无法将内部和外部查询绑定在一起。这是一件容易忘记的事情,如果你没想到的话可以把你赶出去。