请问一下在sql server数据库中,如何用sql语句表示,将一个表中的几个字段都连接起来.
比如,表:ABCname   sex     op1      op2       op3   op4 
----  ----   ------   ------   ------   ------
张红     女    1        2         3        3
大明     男    2        1          4      3
表中的op1,op2,op3,op4字段的值是另一个表中的ID字段值
表:EDF
id    name
---  ------
1       甲
2       乙
3       丙
4       丁
5       戊
6       己我想得到数据的格式是:
张红 女 甲 乙 丙 丙
谢谢

解决方案 »

  1.   

    declare @s varchar(50)
    select @s=isnull(@s+' ','')+name from tb
    print @s
      

  2.   


    select a.name,  a.sex,    b.op1,      c.op2 ,     d.op3,  e.op4,
    from ABC a left join EDF b
    on a.op1 = b.id
    left join EDF c
    on a.op2 = c.id 
    left join EDF d
    on a.op3 = d.id
    left join EDF e
    on a.op4 = e.id
      

  3.   

    if object_id('[ABC]') is not null drop table [ABC]
    go
    create table [ABC]([name] varchar(4),[sex] varchar(2),[op1] int,[op2] int,[op3] int,[op4] int)
    insert [ABC]
    select '张红','女',1,2,3,3 union all
    select '大明','男',2,1,4,3
    if object_id('[EDF]') is not null drop table [EDF]
    go
    create table [EDF]([id] int,[name] varchar(2))
    insert [EDF]
    select 1,'甲' union all
    select 2,'乙' union all
    select 3,'丙' union all
    select 4,'丁' union all
    select 5,'戊' union all
    select 6,'己'select a.name,a.sex,b.name,c.name,d.name,e.name
    from ABC a
    join EDF b on a.op1=b.id
    join EDF c on a.op1=c.id
    join EDF d on a.op1=d.id
    join EDF e on a.op1=e.id
    --测试结果:
    /*
    name sex  name name name name
    ---- ---- ---- ---- ---- ----
    张红   女    甲    甲    甲    甲
    大明   男    乙    乙    乙    乙(2 行受影响)*/
      

  4.   

    修正
    select a.name,a.sex,b.name,c.name,d.name,e.name
    from ABC a
    join EDF b on a.op1=b.id
    join EDF c on a.op2=c.id
    join EDF d on a.op3=d.id
    join EDF e on a.op4=e.id
    --测试结果:
    /*
    name sex  name name name name
    ---- ---- ---- ---- ---- ----
    张红   女    甲    乙    丙    丙
    大明   男    乙    甲    丁    丙(2 行受影响)*/
      

  5.   

    多次join的这个方法我也想过,就是想知道有没有什么办法可以简单点。再等两天,看看还有别的办法吗,到时结贴。谢谢上面的各位,到时给分。