mysql逗号分隔查询 - 将逗号分隔列值转换为行




group_concat (3)

尝试这个

 SELECT A.[id],  
     Split.a.value('.', 'VARCHAR(100)') AS String  
 FROM  (SELECT [id],  
         CAST ('<M>' + REPLACE([string], ',', '</M><M>') + '</M>' AS XML) AS String  
     FROM  TableA) AS A CROSS APPLY String.nodes ('/M') AS Split(a); 

请参考这里

http://www.sqljason.com/2010/05/converting-single-comma-separated-row.html

https://code.i-harness.com

我有一个表样本 ,数据存储如下

Id   | String
--------------
1     abc,def,ghi
2     jkl,mno,pqr

我需要像...那样的输出

Id   | processedrows
--------------
1     abc
1     def
1     ghi
2     jkl
2     mno
2     pqr

如何在SQL Server中使用select查询执行相同的操作?


让我们试试下面的脚本: -

declare @str varchar(max)

SELECT @str = isnull(@str +',', '') + a.Value
FROM (SELECT Value Entityvalue from Table) a

select @str

SELECT EmployeeID,
LTRIM(RTRIM(m.n.value('.[1]','varchar(8000)'))) AS Certs
FROM
(
SELECT EmployeeID,CAST('<XMLRoot><RowData>' + REPLACE(Certs,',','</RowData><RowData>') + '</RowData></XMLRoot>' AS XML) AS x
FROM   @t
)t
CROSS APPLY x.nodes('/XMLRoot/RowData')m(n)




delimiter