mysql根據逗號將一行數據拆分成多行數據


 

轉自:https://www.cnblogs.com/David3290/p/11378579.html  

SELECT

  a.id,
  a. NAME,
  substring_index(
    substring_index(
    a.shareholder,',',b.help_topic_id + 1),',' ,- 1) AS shareholder
FROM
  company a
  JOIN mysql.help_topic b ON b.help_topic_id < (length(a.shareholder) - length(REPLACE (a.shareholder, ',', '')) + 1 )  SE

 

 

基本建表語句

CREATE TABLE `company` ( 
  `id` int(20) DEFAULT NULL, 
  `name` varchar(100) DEFAULT NULL, 
  `shareholder` varchar(100) DEFAULT NULL 
) ENGINE=InnoDB DEFAULT CHARSET=utf8; 
INSERT INTO `company` VALUES ('1', '阿里巴巴', '馬雲'); 
INSERT INTO `company` VALUES ('2', '淘寶', '馬雲,孫正義');

使用情景:假定a表某一列的數據格式是 逗號分隔 的 a,b,c,想將之變成三行數據

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM