首页 热点专区 小学知识 中学知识 出国留学 考研考公
您的当前位置:首页正文

mysql如何将一行变成多行

2020-11-09 来源:要发发知识网

mysql将一行变成多行的方法:

44c937c8cb0c09476feb5eb1491e43b.png

转成

319e6ba2e18fdb554f76d45f15250a0.png

SELECT
 substring_index(substring_index( a.rn,',',b.help_topic_id + 1 ),',' ,- 1) AS rn
FROM
 (select '1,2,3,4' as rn) a 
JOIN mysql.help_topic b ON b.help_topic_id <
(length(a.rn) - length( replace(a.rn, ',', '') ) + 1)

图A转成图B

19275fe886e86f672f00b79d7057937.png

图a

6f3e1d38d92ed1e69418732db8a3097.png

图b

SELECT
 substring_index(substring_index( a.rn,',',b.help_topic_id + 1 ), ',' ,- 1 ) AS rn
FROM
 (select SUBSTR(GROUP_CONCAT( REPEAT(CONCAT(number,','),fre) SEPARATOR ''),1,LENGTH(GROUP_CONCAT( REPEAT(CONCAT(number,','),fre) SEPARATOR ''))-1) as rn from numbers ) a 
JOIN mysql.help_topic b ON b.help_topic_id <
(length(a.rn) - length( replace(a.rn, ',', '') ) + 1)

更多相关免费学习推荐:mysql教程(视频)

显示全文