利用SQL对数据库实行数据拆分与组合

利用SQL对数据库实行数据拆分与组合实现提供以下几种方案:

方法一:

WITH CTE AS (
SELECT A.Id,A.[Uid],UserName FROM (
SELECT A.[id], REPLACE(Split.a.value('.', 'NVARCHAR(max)'),'''','') AS [Uid]
FROM
(
SELECT [id], CAST ('<M>' + REPLACE(Uid, ',', '</M><M>') + '</M>' AS XML) AS [Uid] FROM A
) AS A CROSS APPLY [Uid].nodes ('/M') AS Split(a)

) A INNER JOIN dbo.B B ON A.[Uid]=B.[Uid]
)
SELECT DISTINCT classList=stuff((select ',' +UserName from CTE B WHERE A.Id=B.Id for xml path('')),1,1,'') FROM CTE A

 方法二:

SELECT t.id,classList=STUFF((
SELECT ','+tt1.UserName
FROM A tt left join B tt1 ON ''+tt.Uid+',' like '%'+tt1.Uid+',%'
WHERE tt.id=t.id FOR XML PATH('')),1,1,'')
FROM A t
ORDER BY t.id

方法三:

select id, (select UserName from B where Uid=t02.item) c into #ttt from dbo.A as t01 cross apply dbo.split(t01.Uid,',') as t02

select id,stuff((select ','+c from dbo.#ttt as tb01 where tb01.id=tb02.id for xml path('')),1,1,'') as classList

from dbo.#ttt as tb02 group by id

drop table #ttt

方法三需要添加内置方法:

create function split(
@string varchar(255),
@separator varchar(255)
)returns @array table(item varchar(255))
as
begin
declare @begin int,@end int,@item varchar(255)
set @begin = 1
set @end=charindex(@separator,@string,@begin)
while(@end<>0)
begin
set @item = substring(@string,@begin,@end-@begin)
insert into @array(item) values(@item)
set @begin = @end+1
set @end=charindex(@separator,@string,@begin)
end
set @item = substring(@string,@begin,len(@string)+1-@begin)
if (len(@item)>0)
insert into @array(item) values(substring(@string,@begin,len(@string)+1-@begin))
return
end

下面们直接看结果吧:

表A:

利用SQL对数据库实行数据拆分与组合

表B:

利用SQL对数据库实行数据拆分与组合

最终结果:

利用SQL对数据库实行数据拆分与组合

跟根据讨论给出的方法,我个人给出这样的三种,如果有不对之处,请多多指教。

相关推荐