gpt4 book ai didi

postgresql - 错误 : invalid reference to FROM-clause

转载 作者:行者123 更新时间:2023-11-29 11:08:53 26 4
gpt4 key购买 nike

我有以下 SQL (PostgreSQL) 查询:

SELECT ff.*, fp.*
FROM fibra ff, fibra fp

JOIN cables cp ON fp.cable_id = cp.id
LEFT OUTER JOIN terceiro ced_pai ON ced_pai.id = cp.cedente_id
LEFT OUTER JOIN terceiro tp ON tp.id = fp.terceiro_id

JOIN cables cf ON ff.cable_id = cf.id
LEFT OUTER JOIN terceiro ced_f ON ced_f.id = cf.cedente_id
LEFT OUTER JOIN terceiro tf ON tf.id = ff.terceiro_id

where ff.fibra_pai_id = fp.id
AND ff.cable_id IN (8,9,10)
AND fp.cable_id IN (8,9,10)

但它给了我这个错误:

ERROR:  invalid reference to FROM-clause entry for table "ff"
LINE 8: JOIN cables cf ON ff.cable_id = cf.id
^
HINT: There is an entry for table "ff", but it cannot be referenced from this part of the query.

********** Error **********

ERROR: invalid reference to FROM-clause entry for table "ff"
SQL state: 42P01
Hint: There is an entry for table "ff", but it cannot be referenced from this part of the query.
Character: 261

有谁知道我做错了什么?

最佳答案

您正在混合使用隐式和显式 JOIN。正如您刚刚发现的那样,这通常让人难以阅读,并导致意想不到的求值顺序问题。

你应该始终如一地使用 JOIN ... ON 语法;避免遗留 FROM table1, table2。如果您更正查询以使用显式 JOIN 而不是 FROM fibra ff, fibra fp,例如 FROM fibra ff INNER JOIN fibra fp ON (ff.fibra_pai_id = fp.id) 并从 WHERE 子句中省略 ff.fibra_pai_id = fp.id,您应该会得到预期的结果。

查看 A.H. 链接到的这个问题:

Mixing explicit and implicit joins fails with "There is an entry for table ... but it cannot be referenced from this part of the query"

关于postgresql - 错误 : invalid reference to FROM-clause,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13297491/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com