查询上级、同级、下级

v1ctory1216 2010-11-18 08:48:54
t1
公司 总经理
A 张三
B 李四
t2
公司 部门 部门经理
A 1 王五
A 2 马六
t3
人员 公司 部门
阿一 A 1
阿二 A 2
王五 A 1
t4
编号 指标
1 asdf
2 ffasf
要查询部门经理王五的上级、同级、下级
输出格式为 t3.公司、t3.部门、t3.人员、关系、人员、t4.编号、t4.指标

说明:王五是A公司的1部门负责人,要找到他的上级,就是A公司的总经理,找到他的同级是其他部门的经理,马六。下级就是王五部门内的非王五的员工。
输出格式如上用union连接到一起,上表前4个字段应该是王五的属性。

...全文
285 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
abuying 2010-11-18
  • 打赏
  • 举报
回复
--加上cross join t4
select * from(
select t2.gs,t2.bm,'王五' as jl,'上级' as gx,t1.zjl as ry,t4.bh,t4.zb from t2 inner join t1 on t1.gs=t2.gs
 cross join t4
where t2.bmjl='王五'
union all
select t2.gs,t2.bm,'王五' as jl,'同级'as gx,t2.bmjl as ry,t4.bh,t4.zb from t2  cross join t4 where bmjl <>'王五'
union all
select t2.gs,t2.bm,'王五' as jl,'下级' as gx,t3.ry,t4.bh,t4.zb from t2 inner join t3 on t2.bm=t3.bm  cross join t4 where bmjl='王五' and t3.ry <>'王五'
)t
-晴天 2010-11-18
  • 打赏
  • 举报
回复
1.2楼的那六行数据,马六不是部门1而是部门2的.
2.虽然写出来了,仍然不明白是啥意思...
仅供楼主参考.
-晴天 2010-11-18
  • 打赏
  • 举报
回复
这样?
create table t1(gs varchar(10),zjl nvarchar(10))
insert into t1 select 'A','张三'
insert into t1 select 'B','李四'
create table t2(gs varchar(10),bm int,bmjl nvarchar(10))
insert into t2 select 'A',1,'王五'
insert into t2 select 'A',2,'马六'
create table t3(ry nvarchar(10),gs varchar(10),bm int)
insert into t3 select '阿一','A',1
insert into t3 select '阿二','A',2
insert into t3 select '王五','A',1
create table t4(bh int,zb nvarchar(10))
insert into t4 select 1,'asdf'
insert into t4 select 2,'ffasf'
go
select * from(
select t2.gs,t2.bm,'王五' as jl,'上级' as gx,t1.zjl as ry from t2 inner join t1 on t1.gs=t2.gs where t2.bmjl='王五'
union all
select t2.gs,t2.bm,'王五' as jl,'同级'as gx,t2.bmjl as ry from t2 where bmjl<>'王五'
union all
select t2.gs,t2.bm,'王五' as jl,'下级' as gx,t3.ry from t2 inner join t3 on t2.bm=t3.bm where bmjl='王五' and t3.ry<>'王五'
)t,t4
go
drop table t1,t2,t3,t4
/*
gs bm jl gx ry bh zb
---------- ----------- ---- ---- ---------- ----------- ----------
A 1 王五 上级 张三 1 asdf
A 1 王五 上级 张三 2 ffasf
A 2 王五 同级 马六 1 asdf
A 2 王五 同级 马六 2 ffasf
A 1 王五 下级 阿一 1 asdf
A 1 王五 下级 阿一 2 ffasf

(6 行受影响)

*/
v1ctory1216 2010-11-18
  • 打赏
  • 举报
回复
6楼大哥,你写的什么呀,还有很多数据呢!
v1ctory1216 2010-11-18
  • 打赏
  • 举报
回复
3楼,4楼不对
-晴天 2010-11-18
  • 打赏
  • 举报
回复
create table t1(gs varchar(10),zjl nvarchar(10))
insert into t1 select 'A','张三'
insert into t1 select 'B','李四'
create table t2(gs varchar(10),bm int,bmjl nvarchar(10))
insert into t2 select 'A',1,'王五'
insert into t2 select 'A',2,'马六'
create table t3(ry nvarchar(10),gs varchar(10),bm int)
insert into t3 select '阿一','A',1
insert into t3 select '阿二','A',2
insert into t3 select '王五','A',1
create table t4(bh int,zb nvarchar(10))
insert into t4 select 1,'asdf'
insert into t4 select 2,'ffasf'
go
select t2.gs,t2.bm,'王五' as jl,'上级' as gx,t1.zjl as ry from t2 inner join t1 on t1.gs=t2.gs where t2.bmjl='王五'
union all
select t2.gs,t2.bm,'王五' as jl,'同级'as gx,t2.bmjl as ry from t2 where bmjl<>'王五'
union all
select t2.gs,t2.bm,'王五' as jl,'下级' as gx,t3.ry from t2 inner join t3 on t2.bm=t3.bm where bmjl='王五' and t3.ry<>'王五'
go
drop table t1,t2,t3,t4
/*
gs bm jl gx ry
---------- ----------- ---- ---- ----------
A 1 王五 上级 张三
A 2 王五 同级 马六
A 1 王五 下级 阿一

(3 行受影响)

*/
v1ctory1216 2010-11-18
  • 打赏
  • 举报
回复
这样就明白了
在T4里面加个字段type,在T3里面也加个字段type,每个人都有个type的一个属性,如T3
t3
人员 公司 部门 type
阿一 A 1 1
阿二 A 2 2
王五 A 1 1
t4表为
type编号 指标
1 1 asdf
1 2 ffasf
-晴天 2010-11-18
  • 打赏
  • 举报
回复
如果考虑阿二不是王五部门的,不能算下级,应为:
create table t1(gs varchar(10),zjl nvarchar(10))
insert into t1 select 'A','张三'
insert into t1 select 'B','李四'
create table t2(gs varchar(10),bm int,bmjl nvarchar(10))
insert into t2 select 'A',1,'王五'
insert into t2 select 'A',2,'马六'
create table t3(ry nvarchar(10),gs varchar(10),bm int)
insert into t3 select '阿一','A',1
insert into t3 select '阿二','A',2
insert into t3 select '王五','A',1
create table t4(bh int,zb nvarchar(10))
insert into t4 select 1,'asdf'
insert into t4 select 2,'ffasf'
go
select t2.gs,t2.bm,t1.zjl as ry,'上级' as gx from t2 inner join t1 on t1.gs=t2.gs where t2.bmjl='王五'
union all
select t2.gs,t2.bm,t2.bmjl as ry,'同级'as gx from t2
union all
select t2.gs,t2.bm,t3.ry,'下级' as gx from t2 inner join t3 on t2.bm=t3.bm where bmjl='王五' and t3.ry<>'王五'
go
drop table t1,t2,t3,t4
/*
gs bm ry gx
---------- ----------- ---------- ----
A 1 张三 上级
A 1 王五 同级
A 2 马六 同级
A 1 阿一 下级

(5 行受影响)

*/
-晴天 2010-11-18
  • 打赏
  • 举报
回复
不知道你的编号和指标是啥意思.
create table t1(gs varchar(10),zjl nvarchar(10))
insert into t1 select 'A','张三'
insert into t1 select 'B','李四'
create table t2(gs varchar(10),bm int,bmjl nvarchar(10))
insert into t2 select 'A',1,'王五'
insert into t2 select 'A',2,'马六'
create table t3(ry nvarchar(10),gs varchar(10),bm int)
insert into t3 select '阿一','A',1
insert into t3 select '阿二','A',2
insert into t3 select '王五','A',1
create table t4(bh int,zb nvarchar(10))
insert into t4 select 1,'asdf'
insert into t4 select 2,'ffasf'
go
select t2.gs,t2.bm,t1.zjl as ry,'上级' as gx from t2 inner join t1 on t1.gs=t2.gs where t2.bmjl='王五'
union all
select t2.gs,t2.bm,t2.bmjl as ry,'同级'as gx from t2
union all
select t2.gs,t2.bm,t3.ry,'下级' as gx from t2 inner join t3 on t2.gs=t3.gs where bmjl='王五' and t3.ry<>'王五'
go
drop table t1,t2,t3,t4
/*
gs bm ry gx
---------- ----------- ---------- ----
A 1 张三 上级
A 1 王五 同级
A 2 马六 同级
A 1 阿一 下级
A 1 阿二 下级

(5 行受影响)

*/
v1ctory1216 2010-11-18
  • 打赏
  • 举报
回复
结果
公司 部门 人员 关系 人员 编号 指标
A 1 王五 上级 张三 1 asdf
A 1 王五 上级 张三 2 ffasf
A 1 王五 同级 马六 1 asdf
A 1 王五 同级 马六 2 ffasf
A 1 王五 下级 阿一 1 asdf
A 1 王五 下级 阿一 2 ffasf
华夏小卒 2010-11-18
  • 打赏
  • 举报
回复
结果是什么样

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧