mysql如何將一行變成多行

2020-09-28 12:00:58

mysql將一行變成多行的方法:1、將一行變成多行,程式碼為【substring_index(substring_index( a.rn,',',b.help_topic_id + 1 ),',' ,- 1)】;2、將多列變成多行。

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如何將一行變成多行的詳細內容,更多請關注TW511.COM其它相關文章!