【问题标题】:How to fix my query如何修复我的查询
【发布时间】:2011-10-19 19:13:35
【问题描述】:

我有以下疑问:

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。

知道如何解决这个问题/我的查询有什么问题吗?

【问题讨论】:

标签: php sql postgresql pdo


【解决方案1】:

这个:

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

似乎试图使用table_a 中的列左加入table_ctable_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_ctable_b 的连接条件移到 FROM 子句中,因此您不再需要在 WHERE 子句中使用它。

【讨论】:

    猜你喜欢
    • 2017-09-24
    • 2021-03-12
    • 1970-01-01
    • 2011-12-13
    • 1970-01-01
    • 1970-01-01
    • 2022-11-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多