首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

【一对多】查询,求大神帮帮忙。该如何解决

2012-09-17 
【一对多】查询,求大神帮帮忙。问题描述:现在有两个表,tb_1和tb_2tb_1如下:IDStrucType1单板2Whippletb_2如下

【一对多】查询,求大神帮帮忙。
问题描述:
现在有两个表,tb_1和tb_2

tb_1如下:
IDStrucType
1单板
2Whipple

tb_2如下:
LIDLNameLValue
1靶板1.5
2缓冲屏2.2
2靶板1.8

现在,我想得到结构为Whipple的详细信息,并且得到如下情况:
IDStrucType LName1LValue1 LName2LValue2
2Whipple缓冲屏2.2靶板1.8

[解决办法]

SQL code
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 #tmpselect     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*/ 

热点排行