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

SQL 难题,求解,谢谢!
如下所以,是一张表,想把相同ID的content 链接起来!
Content ID
I 24FA5145 Content ID
am 24FA5145 想得到==> I am johnny 24FA5145
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
------解决方案--------------------
SQL code
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)
as
begin
    declare @sql varchar(1000)
    select @sql=isnull(@sql+' ','') + Content from ll where id=@id
    return @sql
end

select distinct dbo.wsp22(id) as Content,id from ll

------解决方案--------------------
SQL code

DECLARE  @A TABLE(Content VARCHAR(20), ID VARCHAR(20))
insert @A
select '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=@id
set @str=right(@str,len(@str)-1)
SELECT @str AS Content, '24FA5145' AS ID