【问题标题】:Subquery in FROM cannot refer to other relations of same query levelFROM中的子查询不能引用同一查询级别的其他关系
【发布时间】:2015-01-25 13:45:19
【问题描述】:

我有 4 个表,我想根据某些条件从中选择数据:organisationunit,orgunitgroupmembers,orgunitgroup, orgunitgroupsetmember

和选择代码:

SELECT *
FROM   organisationunit,
       orgunitgroupmembers,
       orgunitgroup,
       orgunitgroupsetmembers
WHERE  organisationunit.comment = '1'
       AND orgunitgroupmembers.organisationunitid = organisationunit.organisationunitid
       AND orgunitgroup.orgunitgroupid = orgunitgroupmembers.orgunitgroupid
       AND orgunitgroupsetmembers.orgunitgroupid = orgunitgroup.orgunitgroupid
       AND orgunitgroupsetmembers.orgunitgroupsetid = '15633' 

它确实返回了我想要的,但现在,我想添加这些部分

(select name 
 from organisationunit  tt 
 where tt.organisationunitid =organisationunit.parentid) as rayon

select方法之后:

SELECT *
FROM   organisationunit,
       orgunitgroupmembers,
       orgunitgroup,
       orgunitgroupsetmembers,
       (SELECT NAME
        FROM   organisationunit tt
        WHERE  tt.organisationunitid = organisationunit.parentid) AS rayon
WHERE  organisationunit.comment = '1'
       AND orgunitgroupmembers.organisationunitid = organisationunit.organisationunitid
       AND orgunitgroup.orgunitgroupid = orgunitgroupmembers.orgunitgroupid
       AND orgunitgroupsetmembers.orgunitgroupid = orgunitgroup.orgunitgroupid
       AND orgunitgroupsetmembers.orgunitgroupsetid = '15633' 

它给出了错误

FROM中的子查询不能引用同一查询级别的其他关系

如您所见,表organisationunit 有一个parentid,这是一个数字,同一个表organisationunit 包含该parentid 的名称。所以我想在一列的行尾显示那个parentid的名字。

P.S 我希望你得到我想要的,否则请随时提出问题。到目前为止,这是我第一次进行数据库操作,如果有些事情太明显,相信我不适合我。

【问题讨论】:

    标签: sql postgresql


    【解决方案1】:

    如果没有错,这就是您所需要的。这里我做了一些改动。

    将旧式连接转换为正确的INNER JOIN,并将过滤器移至where 子句。

    接下来如错误中所述,您不能在subqueryfrom 子句中引用其他表。实际上您需要使用correlated subquery 来查找parent nameself join。如果支持,甚至交叉应用

    SELECT *,
           (SELECT NAME
            FROM   organisationunit tt
            WHERE  tt.organisationunitid = organisationunit.parentid) AS rayon
    FROM   organisationunit
           INNER JOIN orgunitgroupmembers
                   ON orgunitgroupmembers.organisationunitid = organisationunit.organisationunitid
           INNER JOIN orgunitgroup
                   ON orgunitgroup.orgunitgroupid = orgunitgroupmembers.orgunitgroupid
           INNER JOIN orgunitgroupsetmembers
                   ON orgunitgroupsetmembers.orgunitgroupid = orgunitgroup.orgunitgroupid
    WHERE  organisationunit.comment = '1'
           AND orgunitgroupsetmembers.orgunitgroupsetid = '15633' 
    

    甚至是self join 到同一个表中以查找父级详细信息。

    SELECT *,
           tt.NAME AS rayon
    FROM   organisationunit
           INNER JOIN orgunitgroupmembers
                   ON orgunitgroupmembers.organisationunitid = organisationunit.organisationunitid
           INNER JOIN orgunitgroup
                   ON orgunitgroup.orgunitgroupid = orgunitgroupmembers.orgunitgroupid
           INNER JOIN orgunitgroupsetmembers
                   ON orgunitgroupsetmembers.orgunitgroupid = orgunitgroup.orgunitgroupid
           INNER JOIN organisationunit tt
                   ON tt.organisationunitid = organisationunit.parentid
    WHERE  organisationunit.comment = '1'
           AND orgunitgroupsetmembers.orgunitgroupsetid = '15633' 
    

    【讨论】:

    • 太快了……我欠你一杯啤酒)。有用。它如何在数据库中调用?这些技术的名称是什么?我想学习如何去做。内联?选择 *?
    • @Daler - 寻找内连接、自连接和相关子查询
    【解决方案2】:

    要获取父级的名称,请使用自联接。在 from 子句中再包含一个对 organizationunit 的引用,使用别名 tt 或其他。像这样的东西:

    select A.*, B.*, C.*, D.*. tt.name as Rayon from organisationunit A,orgunitgroupmembers B,orgunitgroup C, orgunitgroupsetmembers D, organisationunit tt
    where organisationunit.comment='1' 
    and orgunitgroupmembers.organisationunitid=organisationunit.organisationunitid
    and orgunitgroup.orgunitgroupid =orgunitgroupmembers.orgunitgroupid 
    and orgunitgroupsetmembers.orgunitgroupid=orgunitgroup.orgunitgroupid
    and orgunitgroupsetmembers.orgunitgroupsetid='15633'
    and  tt.organisationunitid =A.organisationunit.parentid
    

    当然,您可能要注意,如果数据集很大,自连接可能会出现一些性能问题,在这种情况下,您可以在选择临时表后使用多步操作。

    【讨论】:

      猜你喜欢
      • 2012-06-02
      • 1970-01-01
      • 2022-09-15
      • 1970-01-01
      • 1970-01-01
      • 2019-01-09
      • 2019-06-21
      • 2018-02-24
      • 1970-01-01
      相关资源
      最近更新 更多