求一条SQL语句

liuyjcel 2009-07-13 04:51:26
数据库中有两个表A(主表)和B(子表),结构如下:
A 表:
ID Code Name
1 001 张三
2 002 李四

B 表:
Code Type Result
001 重量 100公斤
001 身高 170CM
002 视力 0.5

使用一条SQL语句,使得结果显示如下:
ID Code Name 重量 身高 视力
1 001 张三 100公斤 170CM
2 002 李四 0.5

...全文
69 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
jy00578880 2009-07-13
  • 打赏
  • 举报
回复
select D.name,D.code,D.身高,D.视力,B.result as 体重 from
(select C.name,C.code,C.身高,B.result as 视力 from
(select A.name,A.code,B.result as 身高
from A left join B on A.code=B.code and type='身高') as C left join B on C.code=B.code and type='视力') as D left join B on D.code=B.code and type='体重'

日,怎么感觉我写出来怪怪的
我记得以前写的时候 也是用3次左外 但是只用了一个select啊
郁闷 忘了怎么弄得了 SQL退步了
zzxap 2009-07-13
  • 打赏
  • 举报
回复


select a.*,b.* from a left join

(

select code,max(case B.[Type] when '重量' then Result else null end)as 重量,
max(case B.[Type] when '身高' then Result else null end)as 身高,
max(case B.[Type] when '视力' then Result else null end)as 视力
from B group by b.code

)b on a.code=b.code
十八道胡同 2009-07-13
  • 打赏
  • 举报
回复
学习
ws_hgo 2009-07-13
  • 打赏
  • 举报
回复
如果对行列装换不清楚
看下我的blog
http://blog.csdn.net/ws_hgo/archive/2009/03/17/3999394.aspx
里面有详细的讲解
jy00578880 2009-07-13
  • 打赏
  • 举报
回复
用3个左外连接也行
ws_hgo 2009-07-13
  • 打赏
  • 举报
回复
动态的
create table #A1([ID] int,[Code] varchar(3),[Name] varchar(4))
insert #A1
select 1,'001','张三' union all
select 2,'002','李四'

create table #B([Code] varchar(3),[Type] varchar(4),[Result] varchar(7))
insert #B
select '001','重量','100公斤' union all
select '001','身高','170CM' union all
select '002','视力','0.5'

select A.ID,A.[Code],A.[Name],
max(case when [Type]='重量' then [Result] else null end) '重量',
max(case when [Type]='身高' then [Result] else null end) '身高',
max(case when [Type]='视力' then [Result] else null end) '视力'
from #A1 A join #B B on A.[Code]=B.[Code] group by A.ID,A.[Code],A.[Name]

declare @sql nvarchar(2000)
set @sql='select A.ID,A.[Code],A.[Name],'
select @sql=@sql+'max(case when [Type]='''+[Type]+'''then [Result] else null end) ['+[Type]+'],' from #A1 A join #B B on A.[Code]=B.[Code]
select @sql=left(@sql,len(@sql)-1)+'from #A1 A join #B B on A.[Code]=B.[Code] group by A.ID,A.[Code],A.[Name]'
exec(@sql)

ID Code Name 重量 身高 视力
----------- ---- ---- ------- ------- -------
1 001 张三 100公斤 170CM NULL
2 002 李四 NULL NULL 0.5
ws_hgo 2009-07-13
  • 打赏
  • 举报
回复
静态的
create table #A1([ID] int,[Code] varchar(3),[Name] varchar(4))
insert #A1
select 1,'001','张三' union all
select 2,'002','李四'

create table #B([Code] varchar(3),[Type] varchar(4),[Result] varchar(7))
insert #B
select '001','重量','100公斤' union all
select '001','身高','170CM' union all
select '002','视力','0.5'

select A.ID,A.[Code],A.[Name],
max(case when [Type]='重量' then [Result] else null end) '重量',
max(case when [Type]='身高' then [Result] else null end) '身高',
max(case when [Type]='视力' then [Result] else null end) '视力'
from #A1 A join #B B on A.[Code]=B.[Code] group by A.ID,A.[Code],A.[Name]
ID Code Name 重量 身高 视力
----------- ---- ---- ------- ------- -------
1 001 张三 100公斤 170CM NULL
2 002 李四 NULL NULL 0.5
mbh0210 2009-07-13
  • 打赏
  • 举报
回复
如果类型是固定的,就用3楼的,类型不固定,那么使用1楼的
asdfg_ 2009-07-13
  • 打赏
  • 举报
回复
1楼比三楼强大
jwdream2008 2009-07-13
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 takako_mu 的回复:]
SQL codecreatetable A(IDintnull,Codenvarchar(3)null,[Name]nvarchar(4)null)insertinto A(ID,Code,[Name])select1,'001','张三'unionallselect2,'002','李四'createtable B(Codenvarchar(3)null,[Type]nvarchar(4)nul¡­
[/Quote]
正解!
takako_mu 2009-07-13
  • 打赏
  • 举报
回复

create table A(ID int null,Code nvarchar(3) null,[Name] nvarchar(4) null)
insert into A(ID,Code,[Name])
select 1,'001','张三'
union all
select 2,'002','李四'

create table B(Code nvarchar(3) null,[Type] nvarchar(4) null,Result nvarchar(7) null)
insert into B(Code,[Type],Result)
select '001','重量','100公斤'
union all
select '001','身高','170CM'
union all
select '002','视力','0.5'

select A.ID,A.Code,A.[Name],max(case B.[Type] when '重量' then Result else null end)as 重量,
max(case B.[Type] when '身高' then Result else null end)as 身高,
max(case B.[Type] when '视力' then Result else null end)as 视力
from A inner join B on A.code=B.code group by A.ID,A.Code,A.[Name]

ID Code Name 重量 身高 视力
----------- ---- ---- ------- ------- -------
1 001 张三 100公斤 170CM NULL
2 002 李四 NULL NULL 0.5

(2 個資料列受到影響)
wl_bdqn 2009-07-13
  • 打赏
  • 举报
回复
楼上正解
jiangshun 2009-07-13
  • 打赏
  • 举报
回复

-----------------------------------------

--> 测试时间:2009-07-13
--> 我的淘宝:http://shop36766744.taobao.com/

--------------------------------------------------

if object_id('[A1]') is not null drop table [A1]
create table [A1]([ID] int,[Code] varchar(3),[Name] varchar(4))
insert [A1]
select 1,'001','张三' union all
select 2,'002','李四'


if object_id('[B2]') is not null drop table [B2]
create table [B2]([Code] varchar(3),[Type] varchar(4),[Result] varchar(7))
insert [B2]
select '001','重量','100公斤' union all
select '001','身高','170CM' union all
select '002','视力','0.5'

select * from [A1]
select * from [B2]

declare @s nvarchar(4000)
Select @s=isnull(@s+',','')+quotename([Type])+'=max(case when [Type]='+quotename([Type],'''')+' then [Result] else '''' end)'
from B2 group by[Type]
exec('select ID,A1.[Code],Name,'+@s+' from A1 join B2 on A1.Code=B2.Code group by A1.[Code],ID,Name')

/*
ID Code Name 身高 视力 重量
----------- ---- ---- ------- ------- -------
1 001 张三 170CM 100公斤
2 002 李四 0.5


*/
drop table A1,B1

62,050

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧