select id ,
工号 ,
调动前部门名称 = (select 部门名称 from relegation where 调动前部门编号 = t.编号) ,
调动前职务 ,
调动后部门名称 = (select 部门名称 from relegation where 调动后部门编号 = t.编号) ,
调动后职务
from relegation t
工号 ,
调动前部门名称 = (select 部门名称 from relegation where 调动前部门编号 = t.编号) ,
调动前职务 ,
调动后部门名称 = (select 部门名称 from relegation where 调动后部门编号 = t.编号) ,
调动后职务
from relegation t
from relegation A,departments B,departments c
where A.调动前部门编号=B.部门名称
and A.调动前部门编号 = C.部门名称
if object_id('[relegation]') is not null drop table [relegation]
go
create table [relegation]([id] int,[工号] varchar(6),[调动前部门编号] varchar(3),[调动前职务] varchar(4),[调动后部门编号] varchar(3),[调动后职务] varchar(4))
insert [relegation]
select 1,'000001','001','经理','002','经理' union all
select 2,'000001','002','经理','003','副总'
if object_id('[departments]') is not null drop table [departments]
go
create table [departments]([编号] varchar(3),[部门名称] varchar(6))
insert [departments]
select '002','人事部' union all
select '003','总务部'---创建视图---
create view rel_dep
as
select
id,
工号,
调动前部门名称=(select 部门名称 from departments where 编号=t.调动前部门编号),
调动前职务,
调动后部门名称=(select 部门名称 from departments where 编号=t.调动后部门编号),
调动后职务
from relegation t---查询---
select * from rel_dep
---结果---
id 工号 调动前部门名称 调动前职务 调动后部门名称 调动后职务
----------- ------ ------- ----- ------- -----
1 000001 NULL 经理 人事部 经理
2 000001 人事部 经理 总务部 副总(所影响的行数为 2 行)