问题描述:
现在有两个表,tb_1和tb_2tb_1如下:
ID StrucType
1 单板
2 Whippletb_2如下:
LID LName LValue
1 靶板 1.5
2 缓冲屏 2.2
2 靶板 1.8 现在,我想得到结构为Whipple的详细信息,并且得到如下情况:
ID StrucType          LName1 LValue1   LName2 LValue2
2 Whipple 缓冲屏 2.2 靶板 1.8

解决方案 »

  1.   

    select a.ID StrucType LName1 LValue1 LName2 LValue2 from tb_1 a, tb_2 b where a.id = b.id and b.LName = '缓冲屏' 
      

  2.   

    select a.ID StrucType LName1 LValue1 LName2 LValue2 from tb_1 a, tb_2 b where a.id = b.id and b.LName = '缓冲屏' 
      

  3.   

    select a.ID StrucType LName1 LValue1 LName2 LValue2 from tb_1 a left join  tb_2 b on  a.id = b.id where  b.LName = '缓冲屏' 
      

  4.   

    select a.ID,StrucType,LName,LValue,mid=identity(int,1,1) into #tmp from tb_1 a left join tb_2 b on a.id = b.lid where a.id=2 
    --select * from #tmp
    select 
        id,structype,
        [LName1]=max(case when [mid]='1' then [Lname] else null end),
        [LValue1]=max(case when [mid]='1' then [LValue] else null end),
        [LName2]=max(case when [mid]='2' then [Lname] else null end),
        [LValue2]=max(case when [mid]='2' then [LValue] else null end) 
    from 
        #tmp 
    group by id,structypeGOdrop table #tmp/*
    id          structype  LName1     LValue1    LName2     LValue2    
    ----------- ---------- ---------- ---------- ---------- ---------- 
    2           Whipple    缓冲屏        2.2        靶板         1.8
    */
      

  5.   

    select a.ID StrucType LName1 LValue1 LName2 LValue2 from tb_1 a, tb_2 b where a.id = b.id and b.LName = '缓冲屏'
      

  6.   

    楼上对的···没错···select a.ID,StrucType,LName,LValue,mid=identity(int,1,1) into #tmp from tb_1 a left join tb_2 b on a.id = b.lid where a.id=2 
    --select * from #tmp
    select 
        id,structype,
        [LName1]=max(case when [mid]='1' then [Lname] else null end),
        [LValue1]=max(case when [mid]='1' then [LValue] else null end),
        [LName2]=max(case when [mid]='2' then [Lname] else null end),
        [LValue2]=max(case when [mid]='2' then [LValue] else null end) 
    from 
        #tmp 
    group by id,structypeGOdrop table #tmp/*
    id          structype  LName1     LValue1    LName2     LValue2    
    ----------- ---------- ---------- ---------- ---------- ---------- 
    2           Whipple    缓冲屏        2.2        靶板         1.8
    */