【发布时间】: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