小问题求助~ 速度!!!!
表
CPMInformation
ID Name Url ......
1 test www.baidu.com
2 test2 www.google.com
表
CPMTags
ID Name
1 aaa
2 bbb
3 ccc
表
CPMinformation_CPMTags
ID TagsID
1 1
1 2
1 3
2 3
想要提取CPMInformation信息.CPMTags.ID和CPMinformation_CPMTags.TagsID关联,
CPMinformation_CPMTags.ID和CPMTags.ID对应,,要求结果如下:
速度...............................
ID Name URL Tag
1 test www.baidu.com aaa,bbb,ccc
2 test2 www.google.com ccc
------解决方案--------------------Create Function F_GetTagsName(@ID Int)
Returns Varchar(8000)
As
Begin
Declare @S Varchar(8000)
Select @S = ' '
Select @S = @S + ', ' + B.Name From CPMinformation_CPMTags A Inner Join CPMTags B On B.ID = A.TagsID Where A.ID = @ID Order By B.ID
Select @S = Stuff(@S, 1, 1, ' ')
Return @S
End
GO
Select
*,
dbo.F_GetTagsName(ID) As Tag
From
CPMInformation
GO