使用简单的查询,我可以做如下操作:
SELECT hobbies FROM peoples_hobbies WHERE person_id = 5;
并获得:
shopping
fishing
coding
但是我只想要一个行和一列的结果:
shopping, fishing, coding
我想在SQL Server中实现这个功能?
使用简单的查询,我可以做如下操作:
SELECT hobbies FROM peoples_hobbies WHERE person_id = 5;
并获得:
shopping
fishing
coding
shopping, fishing, coding
我想在SQL Server中实现这个功能?
SQL Server对于聚合字符串连接的支持不是很好。但是你可以这样做:
select stuff((select ', ' + hobbies
from peoples_hobbies
where person_id = 5
for xml path ('')
), 1, 2, '') as hobbies;