SQL 难题,求解,谢谢!
如下所以,是一张表,想把相同ID的content 链接起来!
Content ID
I 24FA5145 Content ID
am 24FA5145想得到==>I am johnny24FA5145
Johnny 24FA5145
[解决办法]
select a.content + ' ' + b.content + ' ' + c.content
from table a ,
table b ,
table c
where a.id = b.id
and a.id = c.id
[解决办法]
create table ll(Content varchar(50),ID varchar(50))insert into ll select 'I','24FA5145'insert into ll select 'am','24FA5145'insert into ll select 'Johnny','24FA5145'create function wsp22(@id varchar(50))returns varchar(1000)asbegin declare @sql varchar(1000) select @sql=isnull(@sql+' ','') + Content from ll where id=@id return @sqlendselect distinct dbo.wsp22(id) as Content,id from ll
[解决办法]
DECLARE @A TABLE(Content VARCHAR(20), ID VARCHAR(20))insert @Aselect 'I','24FA5145' UNION ALL SELECT 'am','24FA5145' UNION ALL SELECT 'Johnny','24FA5145' DECLARE @id VARCHAR(20)SET @id='24FA5145'declare @str varchar(8000)set @str=''select @str=@str+' '+Content from @A where id=@idset @str=right(@str,len(@str)-1)SELECT @str AS Content, '24FA5145' AS ID