日期:2014-05-18  浏览次数:20404 次

SQL 表格式转化 求高手指点
现在情况是这样的
有一张表A
xh lx mc
1 1 谢
1 2 谢
1 3 高
1 4 手
2 1 不
2 2 吝
2 3 赐
2 4 教
现在想把这张表转化为下面的形式
xh mc
1 谢谢高手
2 不吝赐教

这样形式 求教应该怎么实现 
SQL新手 真心求助 谢谢啦

------解决方案--------------------
SQL code
if object_id('[A]') is not null drop table [A]
go
create table [A]([xh] int,[lx] int,[mc] varchar(2))
insert [A]
select 1,1,'谢' union all
select 1,2,'谢' union all
select 1,3,'高' union all
select 1,4,'手' union all
select 2,1,'不' union all
select 2,2,'吝' union all
select 2,3,'赐' union all
select 2,4,'教'
go

select xh,
  mc=(select ''+mc from A where xh=t.xh for xml path(''))
from A t
group by xh

/**
xh          mc
----------- -----------------------
1           谢谢高手
2           不吝赐教

(2 行受影响)
**/