select id ,  
       工号 , 
       调动前部门名称 = (select 部门名称 from relegation where 调动前部门编号 = t.编号) ,
       调动前职务     ,
       调动后部门名称 = (select 部门名称 from relegation where 调动后部门编号 = t.编号) ,
       调动后职务 
from relegation t

解决方案 »

  1.   

    select A.id,A.工号,B.部门名称,A.调动前职务,C.部门名称  ,A.调动后职务
    from relegation A,departments B,departments c
    where A.调动前部门编号=B.部门名称  
     and A.调动前部门编号 = C.部门名称
      

  2.   

    ---测试数据---
    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 行)
      

  3.   

    我用rucypli的方法测试可行.可能是因为我用的是VFP 9的原因,用dawugui和josy的方法提示出错,不过还是要感谢各位朋友的帮忙.