mysql根據逗號將一行數據拆分成多行數據
1、原始數據演示
2、處理結果演示
sql 語句
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 )
建立基礎表
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', '淘寶', '馬雲,孫正義');
解釋:
如果需要使用即
1、切換對應的分隔符,這里用的是逗號
2、切換對應的對應的表,這里用的是 company
3、切換對應的對應的字段,這里用的是 shareholder
4、切換對應的關聯字段 id