How to fix my query
I have the following query:
SELECT table_a.field1, table_b.field1
FROM table_a, table_c
LEFT JOIN tab开发者_如何学JAVAle_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 = ?
However when executed I get the following error:
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.
I'm using PostgreSQL and PDO.
Any idea how to fix this / what's wrong with my query?
This:
FROM table_a, table_c
LEFT JOIN table_b
ON table_a.field1=table_b.field1
appears to be trying to left-join table_c
and table_b
using a column in table_a
and that doesn't make that much sense. Try rewriting the whole FROM like this:
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
Also note that I've moved the join condition for table_c
and table_b
into the FROM clause so you won't need it in the WHERE clause anymore.
精彩评论