0
SELECT sessions_compare.*
    FROM archive_sessions as f_session, sessions_compare
    LEFT JOIN archive_sessions as s_session ON (s_session.id = sessions_compare.second_session_id)
    LEFT JOIN consols as f_consol ON (f_session.console_id = f_consol.id)

    where sessions_compare.first_session_id = f_session.id

在执行 get 错误后

错误:对表“f_session”的 FROM 子句条目的无效引用第 13 行:
LEFT JOIN consols as f_consol ON (f_session.console_id =...

提示:表“f_session”有一个条目,但不能从这部分查询中引用它。

当我切换位置并遇到from类似的sessions_compare, archive_sessions as f_session 错误时

错误:对表“sessions_compare”第 4 行的 FROM 子句条目的无效引用:...archive_sessions as s_session ON (s_session.id = sessions_c...

提示:表“sessions_compare”有一个条目,但不能从这部分查询中引用它。

唯一有效的是

SELECT sessions_compare.*
    FROM sessions_compare
    LEFT JOIN archive_sessions as s_session ON (s_session.id = sessions_compare.second_session_id)

    ,archive_sessions as f_session

    LEFT JOIN consols as f_consol ON (f_session.console_id = f_consol.id)
    where sessions_compare.first_session_id = f_session.id

我的问题是正常吗?当使用来自表的多个连接时,我在 mysql 中的 Postgresql 中很年轻()

4

2 回答 2

2

将主查询更改为 JOIN(并添加/缩短一些相关名称)似乎可以解决问题。

DROP SCHEMA fuzz CASCADE;
CREATE SCHEMA fuzz;
SET search_path='fuzz';

create table archive_sessions ( id INTEGER NOT NULL
 , console_id INTEGER NOT NULL, game_id INTEGER NOT NULL);
create table sessions_compare (first_session_id INTEGER NOT NULL
 , second_session_id INTEGER NOT NULL);
create table consols (id INTEGER NOT NULL);
create table games (id INTEGER NOT NULL);
create table localizations ( consols_id INTEGER NOT NULL, bars_id INTEGER NOT NULL);
create table bars ( id_bars INTEGER NOT NULL, area_id INTEGER NOT NULL);
create table areas ( id_areas INTEGER NOT NULL );

SELECT sco.*
    FROM archive_sessions as ase
    JOIN sessions_compare sco ON sco.first_session_id = ase.id

    LEFT JOIN archive_sessions as ss ON (ss.id = sco.second_session_id)
    LEFT JOIN consols as sc ON (ss.console_id = sc.id)
    LEFT JOIN games as sg ON (ss.game_id = sg.id)
    LEFT JOIN localizations as sl ON (sc.id = sl.consols_id)
    LEFT JOIN bars as sb ON (sl.bars_id = sb.id_bars)
    LEFT JOIN areas as sa ON (sb.area_id = sa.id_areas)



    LEFT JOIN consols as fc ON (ase.console_id = fc.id)
    LEFT JOIN games as fg ON (ase.game_id = fg.id)
    LEFT JOIN localizations as fl ON (fc.id = fl.consols_id)
    LEFT JOIN bars as fb ON (fl.bars_id = fb.id_bars)
    LEFT JOIN areas as fa ON (fb.area_id = fa.id_areas)

    -- WHERE sco.first_session_id = ase.id
        ;
于 2012-04-18T10:27:37.557 回答
2

是的,这是 PostgreSQL 中记录的行为。

在任何情况下,JOIN 比分隔 FROM 项的逗号绑定得更紧密。

这意味着 PostgreSQL 的行为就像它通过在评估 FROM 子句中的逗号列表之前评估 JOIN 子句来构建其工作表一样。

最佳实践通常被认为是始终使用 JOIN 子句;永远不要在 FROM 子句中放置多个表名。

于 2012-04-18T10:37:09.297 回答