我有以下查询:
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。
任何想法如何解决这个/我的查询有什么问题?
答案 0 :(得分:3)
此:
FROM table_a, table_c
LEFT JOIN table_b
ON table_a.field1=table_b.field1
似乎是在尝试使用table_c
中的列离开加入table_b
和table_a
,但这并没有多大意义。尝试重写整个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子句中的连接条件。