A 表 有
id 科目 
1  语文
2  数学
b表有
姓名 科目ID1 科目ID2
张三 1
李四 1       2
想要的结果
C表
姓名  科目1  科目2
张三   语文
李四   语文   数学
如何实现

解决方案 »

  1.   


    select t1.姓名,t2.科目 AS 科目1,t3.科目 AS 科目2  from B t1 
    left join A t2 on t1.科目ID1=t2.id
    left join A t3 on t1.科目ID2=t3.id
      

  2.   

    select 
    姓名, 
    科目ID1 =isnull((select top 1 科目 from A where id=b.科目ID1),''),
    科目ID2 =isnull((select top 1 科目 from A where id=b.科目ID2),'')
    from b
      

  3.   

    select t1.姓名,t2.科目 AS 科目1,t3.科目 AS 科目2  from B t1 
    left join A t2 on t1.科目ID1=t2.id
    left join A t3 on t1.科目ID2=t3.id
      

  4.   

    select 
    姓名, 
    科目ID1 =isnull((select top 1 科目 from A where id=b.科目ID1),''),
    科目ID2 =isnull((select top 1 科目 from A where id=b.科目ID2),'')
    from b