sql语句查询员工的上级,在同一张表中
表是employees(Eid(自己的id),ename,ebirth,epid(上级id));查找名字是me的上级和上上级...
表是employees(Eid(自己的id),ename,ebirth,epid(上级id));
查找名字是me的上级和上上级 展开
查找名字是me的上级和上上级 展开
展开全部
如果是SQL2005以上则可以用CTE递归写法
use tempdb
go
create table #emp
(eid integer not null,
ename varchar(10) not null,
epid integer null,
)
insert into #emp
select 1,'A',5
union all
select 2,'me',3
union all
select 3,'bos',4
union all
select 4,'boss',null
select * from #emp
;with empcte as
(select epid,(select ename from #emp b where b.eid = a.epid) as name from #emp a where ename = 'me'
union all
select b.epid,(select ename from #emp where eid = b.epid) as name from empcte a join #emp b on a.epid = b.eid
)
select * from empcte where epid is not null
use tempdb
go
create table #emp
(eid integer not null,
ename varchar(10) not null,
epid integer null,
)
insert into #emp
select 1,'A',5
union all
select 2,'me',3
union all
select 3,'bos',4
union all
select 4,'boss',null
select * from #emp
;with empcte as
(select epid,(select ename from #emp b where b.eid = a.epid) as name from #emp a where ename = 'me'
union all
select b.epid,(select ename from #emp where eid = b.epid) as name from empcte a join #emp b on a.epid = b.eid
)
select * from empcte where epid is not null
展开全部
是查上级和上上级的ID吗,如果是语句
SELECT t1.Eid, t1.ename, t1.epid, t2.epid as sepid FROM employees t1 INNER JOIN employees t2 ON t1.epid = t2.Eid WHERE t1.ename='me'
SELECT t1.Eid, t1.ename, t1.epid, t2.epid as sepid FROM employees t1 INNER JOIN employees t2 ON t1.epid = t2.Eid WHERE t1.ename='me'
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我去,都这么猛。不过真正的写法是用递归查询。在ORACLE中,写法还算简单。自己查吧,养成一个自己动手解决问题的习惯,真的。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询