sqlserver函数group_concat
时间: 2023-09-03 07:14:41 浏览: 130
GROUP_CONCAT函数-MySQL开发指南
在SQL Server中,没有内置的group_concat函数。但是,你可以使用FOR XML PATH('')语句来实现类似的功能。下面是一个示例:
SELECT
t.name,
STUFF((
SELECT ',' + t2.column_name
FROM table_name t2
WHERE t2.group_column = t.group_column
FOR XML PATH('')
), 1, 1, '') AS concatenated_values
FROM table_name t
GROUP BY t.name
在上面的查询中,我们使用了FOR XML PATH('')语句来将同一组中的值连接起来,并使用STUFF函数来去除第一个逗号。请将"table_name"替换为你的表名,"column_name"替换为需要拼接的列名,"group_column"替换为用于分组的列名。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [SQL Server实现group_concat功能](https://blog.csdn.net/qq_39207963/article/details/130563914)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *2* *3* [SQL Server实现group_concat函数](https://blog.csdn.net/qq_51534884/article/details/129086548)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文