我正在尝试从数据库中检索两条记录,检索当前部门和负责该部门的部门。
例如,部门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的部门信息的详细信息?
答案 0 :(得分: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
答案 1 :(得分: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