本文介绍了如何选择一对多的行到一列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何选择备用模型表中的模型列
备用= 1

how to select model column form spare model table
where spare =1

Spare   Model
1       5
1       6
1       7
2       1  
2       8
2       6



结果集如下所示



result set come as given below

spare  Model
1       5,6,7



感谢



thanks

推荐答案

DECLARE @Spare INT
DECLARE @combinedString NVARCHAR(MAX)

SET @Spare = 1 
SET @combinedString = ''

SELECT @combinedString = COALESCE(@combinedString + ', ', '') + Model
FROM SpareModelTable
WHERE Spare = @Spare


SELECT @Spare AS Spare, @combinedString AS Model


select distinct spare, (select model + ','  from tbl where spare=a.spare for xml path ('')) from tbl as a


祝您编码愉快!
:)


Happy Coding!
:)


这篇关于如何选择一对多的行到一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 05:12