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

Subquery in FROM cannot refer to other relations of same query level

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

和select代码:

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' 

它做了 return 我想要的,但现在,我想添加这些部分

(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' 

它给出了错误

Subquery in FROM cannot refer to other relations of same query level

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

的名称

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

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

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

Next 如错误所述,您不能在 subqueryfrom 子句中引用其他 table。实际上你需要使用 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 到相同的 table 以查找父详细信息。

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' 

要获取父项的名称,请使用自联接。在 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

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