首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

大家都来看看,看哪位高手能做出来

2012-02-03 
大家都来看看,看谁能做出来表idiIDcontent1100a2100b3101c4101d5101e.........要求实现这样的结果iIDconte

大家都来看看,看谁能做出来

id iID content
1 100 a
2 100 b
3 101 c
4 101 d
5 101 e
. . .
. . .
. . .

要求实现这样的结果

iID content
100 a,b
101 c,d,e
. .
. .
. .

[解决办法]

SQL code
--创建环境create table test(iID int,content varchar(100))insert into  test  select 100,'a'              union select 100,'b'             union select 101,'c'             union select 101,'d'             union select 101,'e'--创建一个合并的函数create function contentDo(@id int)returns varchar(8000)asbegindeclare @str varchar(8000)set @str=''select @str=@str+','+cast(content as varchar) from test where iID=@id set @str=right(@str,len(@str)-1)return(@str)Endgo--调用自定义函数得到结果select  iID,max(dbo.contentDo(iID)) as content  from test group by iID--查询结果iID   content  ------------100   a,b    101   c,d,e 

热点排行