记录,以免忘记
原始数据如图所示:
处理一:将time列的内容加[],并且链接为一行
代码为:
SELECT '[ '+time+' ]' FROM test1 FOR XML PATH('')
SELECT '[ '+time+' ]' FROM test1 FOR XML PATH('Time')
结果为:
处理二:将uid,mid以XML的格式存储,xml文件的行节点别名为uid_mid,uid,mid的别名分别为u,m
代码:
select uid as u, mid as m from test1 for xml path('uid_mid')
结果:
<img src="https://img-blog.csdn.net/20150903120442485" alt="" />
处理四:将同一uid的所有微博id拼接成一行,用逗号隔开,以uid分组,并且插入到新表test2中
代码为:<pre name="code" class="sql">if OBJECT_ID('test2', 'u') is not null
drop table test2
select uid,
(SELECT mid+',' FROM test1
WHERE uid=A.uid
FOR XML PATH('')) as mid_new
into test2
from test1 A
group by uid
结果为:<img src="https://img-blog.csdn.net/20150903120613646" alt="" />
处理五:将同一uid的所有微博id拼接成一行,用逗号隔开,以uid分组,并且插入到新表test3中,与上面的区别在于select出来的内容没有最后的分隔符‘,’
代码为:<pre name="code" class="sql">if OBJECT_ID('test3', 'u') is not null
drop table test2
select B.uid, LEFT(mid_new,LEN(mid_new)-1) as Mid_New
into test3
from
(select uid,
(SELECT mid+',' FROM test1
WHERE uid=A.uid
FOR XML PATH('')) as mid_new
from test1 A
group by uid) B
结果为:<img src="https://img-blog.csdn.net/20150903121022859" alt="" />