myMySQL数据库如何将一行变成多行 MySQL数据库使用教程

凤凰 2021-1-19 97 1/19

mysql将一行变成多行的方法:1、将一行变成多行,代码为【substring_index(substring_index( a.rn,',',b.help_topic_id + 1 ),',' ,- 1)】;2、将多列变成多行。

myMySQL数据库如何将一行变成多行 MySQL数据库使用教程

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

myMySQL数据库如何将一行变成多行 MySQL数据库使用教程

转成

myMySQL数据库如何将一行变成多行 MySQL数据库使用教程

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

myMySQL数据库如何将一行变成多行 MySQL数据库使用教程

图a

myMySQL数据库如何将一行变成多行 MySQL数据库使用教程

图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教程(视频)

以上就是mysql如何将一行变成多行的详细内容,更多请关注本站其它相关文章!

- THE END -

凤凰

1月19日09:28

最后修改:2021年1月19日
0

非特殊说明,本博所有文章均为博主原创。