1

我正在尝试从数据库中检索两条记录,该数据库检索当前部门和负责该部门的部门。

例如,部门 id 13 由部门 id 18 管理

select dm.* from TblDepartment d 
join TblDepartment dm 
on d.ManagedBy = dm.id or d.id = dm.ManagedBy
where d.id = 13

我需要使用哪个联接来检索部门 13 和部门 18 的部门信息的详细信息?

4

2 回答 2

1

两排

select 'Dept' as [type], * from 
TblDepartment d 
where d.id = 13
union all
select ' managed by' as [type], * from 
TblDepartment d 
where d.id = 18

或者使用一个连接和一个输入:

select 'Dept' as [type], * from 
TblDepartment d 
where d.id = 13
union all
select ' managed by' as [type], m.* from 
TblDepartment d 
join TblDepartment m on d.ManagedBy = m.id and d.id = 13
于 2013-07-26T10:05:10.277 回答
1

尝试以下操作:

select d.id Dept_Id, d.ManagedBy 
from 
    TblDepartment d 
where
    d.id = 13
union
select dm.id Dept_Id , dm.ManagedBy
from 
    TblDepartment d 
inner 
    join TblDepartment dm 
on  d.ManagedBy = dm.id
where 
    d.id = 13
于 2013-07-26T10:23:03.383 回答