我有以下查询:
SELECT table_a.field1, table_b.field1
FROM table_a, table_c
LEFT JOIN table_b
ON table_a.field1=table_b.field1
WHERE table_a.field2 LIKE ?
AND table_a.field3 = ?
AND table_a.field4 = ?
AND table_b.field1 IS NULL
AND table_c.id = table_b.c_id
AND table_c.field1 = ?
AND table_c.field2 = ?
AND table_c.field3 = ?
但是在执行时出现以下错误:
o: SQLSTATE[42P01]: Undefined table: 7 ERROR: invalid reference to FROM-clause entry for table "table_a" at character 114
HINT: There is an entry for table "table_a", but it cannot be referenced from this part of the query.
我正在使用 PostgreSQL 和 PDO。
知道如何解决这个问题/我的查询有什么问题吗?
最佳答案
这个:
FROM table_a, table_c
LEFT JOIN table_b
ON table_a.field1=table_b.field1
似乎试图使用 table_a
中的列左连接 table_c
和 table_b
并且这没有多大意义。尝试像这样重写整个 FROM:
FROM table_a
LEFT JOIN table_b ON table_a.field1 = table_b.field1
JOIN table_c ON table_c.id = table_b.c_id
另请注意,我已将 table_c
和 table_b
的连接条件移动到 FROM 子句中,因此您将不再需要在 WHERE 子句中使用它。
关于php - 如何修复我的查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6938668/