[英]SQL Resolve foreign key from same table
I was hoping you could help a beginner with something. 我希望您能为初学者提供一些帮助。 I got a query that references several tables.
我得到一个引用几个表的查询。 Two of the tables I'm referencing hold a foreign key, which would be a different records in a joint table.
我要引用的两个表拥有一个外键,这将是联合表中的不同记录。
Example here would be orgunit.shortname. 这里的示例是orgunit.shortname。 This filed would be called twice, but in different relations.
该字段将被调用两次,但关系不同。
I'm sure that there is a way to do this without using a temp table, but I kept failing with several attempts. 我确定有一种方法可以不使用临时表来执行此操作,但是我多次尝试都失败了。
Any help would really be appreciated. 任何帮助将不胜感激。
Best Dennis 最佳丹尼斯
The current query is below. 当前查询如下。
select
orgunit.shortname AS OnsiteDept,
department.shortname AS DeptName,
OrgUnitParent.orgunitid AS ParentID,
Orgunit.xrefcode AS OrgXref,
Orgunit.departmentid AS DeptID,
Orgunit.shortname AS Parentshotname,
Orgunit.orglevelid AS OrgLevel
from orgunit
left join department
on orgunit.departmentid = department.departmentid
left join OrgUnitParent
on orgunit.orgunitid = OrgUnitParent.orgunitid
AND OrgUnitParent.orgunitid = orgunit.orgunitid
where orgunit.shortname
select ou.shortname AS OnsiteDept,
d.shortname AS DeptName,
oup.orgunitid AS ParentID,
ou.xrefcode AS OrgXref,
ou.departmentid AS DeptID,
oup.shortname AS Parentshotname,
ou.orglevelid AS OrgLevel
from orgunit ou left join
department d
on ou.departmentid = d.departmentid left join
OrgUnitParent
on oup.orgunitid = ou.parent_orgunitid left join
orgunit oupp
on oupp.orgunitid = oup.orgunitid
where ou.shortname;
The join between an org unit and its parent is not obvious. 组织单位与其父单位之间的联接并不明显。 Perhaps there is some sort of parent id.
也许有某种父母身份。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.