表1(基础数据表): 条目类别(category) 条目代码(code) 条目名称(name) (category、code 为联合主键)
      文化程度                 1             本科
      文化程度                 2             硕士
      政治面貌                 1             群众
      政治面貌                 2             党员
表2(员工表): 员工编号(employeeid) 文化程度(degree) 政治面貌(party)(employeeid 主键)
                         1000                      1          1
                         1001                      2          2
怎样得到如下视图???
      
      员工编号        文化程度     政治面貌
       1000             本科         群众
       1001             硕士         党员 联合主键外键基础数据

解决方案 »

  1.   

    select employeeid,(select name from 基础数据表 where category = '文化程度' and code = degree) whcd,(select name from 基础数据表 where category = '政治面貌' and code = party) zzmm
    from 员工表
      

  2.   


    if OBJECT_ID('tb1')is not null
    drop table tb1
    if OBJECT_ID('tb2')is not null
    drop table tb2
    go
    create table tb1(category varchar(10),code int,name varchar(10),
    constraint pk_tb1 primary key (category,code))
    create table tb2(employeeid int,degree int,party int,
    constraint pk_tb2 primary key (employeeid))
    insert into tb1
    select    '文化程度',1,'本科' union
    select    '文化程度',2,'硕士' union
    select    '政治面貌',1, '群众' union
    select    '政治面貌', 2,  '党员' 
    insert into tb2
    select      1000,1,1 union
    select     1001,2,2
    select employeeid,文化程度=(select name from tb1 where category+CAST(code as varchar)='文化程度'+CAST(degree as varchar))
    ,政治面貌=(select name from tb1 where category+CAST(code as varchar)='政治面貌'+CAST(party as varchar))
    from tb2
      

  3.   


    if OBJECT_ID('tb1')is not null
    drop table tb1
    if OBJECT_ID('tb2')is not null
    drop table tb2
    go
    create table tb1(category varchar(10),code int,name varchar(10),
    constraint pk_tb1 primary key (category,code))
    create table tb2(employeeid int,degree int,party int,
    constraint pk_tb2 primary key (employeeid))
    insert into tb1
    select    '文化程度',1,'本科' union
    select    '文化程度',2,'硕士' union
    select    '政治面貌',1, '群众' union
    select    '政治面貌', 2,  '党员' 
    insert into tb2
    select      1000,1,1 union
    select     1001,2,2
    select employeeid,tb1.name 文化程度,t2.name 政治面貌
    from tb2 inner join 
    (select * from tb1 where category='文化程度')tb1 on tb1.code=tb2.degree
    inner join 
    (select * from tb1 where category='政治面貌')t2 on t2.code=tb2.degree
      

  4.   

    表1(基础数据表)这个表应该有一个主键,自增主键或者其他自定义主键
    这样sql语句会更简单,查询效率会更高