MySQL的行轉列

1.原始數據演示mysql

 

 2.處理結果演示sql

3.對應的sql語句blog

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
)

4.附錄
基本表創建語句 :string

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', '淘寶', '馬雲,孫正義');
相關文章
相關標籤/搜索