表
A B
1 a
2 b
3 c
想把B字段全部查询出来,得到结果:a,b,c
以逗号分割。
A B
1 a
2 b
3 c
想把B字段全部查询出来,得到结果:a,b,c
以逗号分割。
- SQL code
-
if not object_id('tb') is null drop table tb Go Create table tb([A] int,[B] nvarchar(1)) Insert tb select 1,N'a' union all select 2,N'b' union all select 3,N'c' Go Select distinct stuff((select ','+[B] from tb for xml path('')),1,1,'') from tb t /* a,b,c */
本文介绍了一种使用SQL查询特定字段并以逗号分隔结果的方法。通过创建临时表并利用STUFF函数结合FOR XML PATH实现字符串拼接,最终返回了期望的数据格式:a,b,c。
31万+

被折叠的 条评论
为什么被折叠?



