sql - 混合显式和隐式连接失败,返回 "There is an entry for table ... but it cannot be referenced from this part of the query"

标签 sql postgresql

SELECT
      i.*, 
      r.name AS roomname, 
      c.name AS cat, 
      p.key AS imgkey, 
      p.extension AS imgext
   FROM 
      items i, 
      rooms r, 
      categories c 
         LEFT JOIN photos p 
            ON p.referencekey = i.key 
   WHERE 
          i.room = r.key 
      AND r.key = 663308 
      AND i.sitekey = 32201 
      AND c.key = i.categorykey

上面的查询在执行时返回以下错误。

ERROR: invalid reference to FROM-clause entry for table "i"

LINE 1: ...tegory c LEFT JOIN photos p ON p.referencekey = i.key WHER...

HINT: There is an entry for table "i", but it cannot be referenced from this part of the query.

最佳答案

SQL 规范声明显式连接在隐式连接之前执行。这是一个隐式连接:

FROM table1 t1, table2 t2 WHERE t1.id=t2.t1id

这是一个显式连接:

FROM table1 t1 JOIN table2 t2 ON (t1.id=t2.t1id)

这段代码位:

categories c 
     LEFT JOIN photos p 
        ON p.referencekey = i.key 

是显式连接,首先运行。请注意,此时表别名为 i 还没有被查看,所以它还不能连接。请注意,我相信 MySQL 在 5.2 中修复了此行为,并且此查询也将不再适用。

关于sql - 混合显式和隐式连接失败,返回 "There is an entry for table ... but it cannot be referenced from this part of the query",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6347897/

相关文章:

SQL/BIGQUERY 运行平均值与日期中的 GAP

mysql - 平均从子查询返回的列

sql - postgresql - 日期时间的列和按小时分组的总和

PostgreSQL 将尾随零添加到数字

sql - Postgres函数将两个具有重叠键的json对象合并为一个对象

INSERT INTO 语句中的 PHP 语法错误。SQLExecDirect 中的 SQL 状态 37000

MySQL - ODBC 连接失败,Workbench 连接正常

sql - 如何增加 SQL 查询/存储过程的百分比?

sql - 使用数据类型是 postgresql SET 语句

sql - 没有行的 Timescaledb time_bucket 获取周期