关于sql 单行字符串转多行
mysql:
SELECT id,substring_index( substring_index( A.字段名, ‘,’, b.help_topic_id + 1 ), ‘,’,- 1 ) as A.字段名 FROM 表A JOIN mysql.help_topic b ON b.help_topic_id < ( length( A.字段名 ) - length( REPLACE ( A.字段名, ‘,’, ‘’ ) ) + 1 ) WHERE A.id = ?
备注: oracle 可采取类似方式
sqlsever:
SELECT a.id,b.aaa
FROM (SELECT id,aaa=CAST(’’+REPLACE(CONVERT(varchar(100),aaa),’,’,’’)+’’ AS xml) FROM formtable_main_96 where aaa is not null and id=‘8057’) a
OUTER APPLY (SELECT aaa=T.C.value(’.’,‘varchar(50)’) FROM a.aaa.nodes(’/v’) AS T©) b
备注: 为什么sqlsever 用下边方式,应为写的时候sqlsever 没有substing_index函数,采用截取的方式substring(字符串,start,length)+charindex 用起来比较费事
from: 笔记