如何修复我的查询

时间:2011-08-04 09:00:36

标签: php sql postgresql pdo

我有以下查询:

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。

任何想法如何解决这个/我的查询有什么问题?

1 个答案:

答案 0 :(得分:3)

此:

FROM table_a, table_c
LEFT JOIN table_b
ON table_a.field1=table_b.field1

似乎是在尝试使用table_c中的列离开加入table_btable_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_ctable_b的连接条件移动到FROM子句中,因此您不再需要WHERE子句中的连接条件。