Postgres子查询中潜在的错误?

时间:2019-05-14 12:17:41

标签: sql postgresql

我们在postgres 10和11上运行以下select语句,并在处理子查询时注意到意外行为

查看示例:

更正查询

->子查询中存在子查询列名

select * from information_schema."tables" t
where t.table_schema in (select schemaname from pg_tables t2);

无列模式名2错误

->两个表中都不存在子查询列名

select * from information_schema."tables" t
where t.table_schema in (select schemaname2 from pg_tables t2);

无错误,尽管pg_tables中不存在table_schema列

->子查询中不存在子查询列名,但在主查询中->选择了主查询中的所有行

select * from information_schema."tables" t
where t.table_schema in (select table_schema from pg_tables t2);

由于使用表别名而导致错误

->我们建议的解决方法->始终使用别名,然后迫使postgres验证列名

select * from information_schema."tables" t
where t.table_schema in (select t2.table_schema from pg_tables t2);

特别感谢https://stackoverflow.com/users/8579285/oved-s进行验证和持续支持

0 个答案:

没有答案