逗号在mysql中将值分隔成行

时间:2016-06-02 07:50:02

标签: mysql csv split rows

SELECT DISTINCT jp.skills
FROM job_profile jp
UNION
SELECT js.skills
FROM job_seeker_profile js

结果:

|skills              |
|php                 |
|PHP,Jquery,MVC      | 
|java                |
|.net                |   
|Tally               | 
|php, mysql, yii     |
|css, html, bootstrap|
|javascript, json    |

但我需要将每个项目作为行(每个逗号分隔值作为行)

预期结果:

|skills   | 
|yii      |
|PHP      | 
|Jquery   |
|MVC      |
|.net     |   
|Tally    | 
|bootstrap|
|css      |
|html     |

1 个答案:

答案 0 :(得分:0)

创建一个拆分函数,它将拆分数据,然后创建行式数据

SELECT distinct  Split.fn.value('.', 'VARCHAR(100)') AS skills  
 FROM  
 (SELECT  CAST ('<a>' + REPLACE(skills, ',', '</a><a>') + '</a>' AS XML) AS Data  
  FROM  job_profile
 ) AS A CROSS APPLY Data.nodes ('/a') AS Split(fn); 

注意:根据需要更新内部查询

已更新:在您的情况下,查询将是

SELECT distinct  Split.fn.value('.', 'VARCHAR(100)') AS skills  
 FROM  
 (SELECT  CAST ('<a>' + REPLACE(jp.skills, ',', '</a><a>') + '</a>' AS XML) AS Data  
  FROM  job_profile jp
  UNION
    SELECT js.skills
    FROM job_seeker_profile js
 ) AS A CROSS APPLY Data.nodes ('/a') AS Split(fn);