码迷,mamicode.com
首页 > 其他好文 > 详细

逗号分隔字段,序列化表方法实现列转行

时间:2020-02-02 13:55:59      阅读:65      评论:0      收藏:0      [点我收藏+]

标签:tab   sel   into   com   gif   sequence   cat   nbsp   auto   

逗号分隔字段,序列化表方法实现列转行

 

qujing表:

技术图片

 

 

创建序列表:

create table tb_sequence(id int auto_increment not null,primary key(id));
insert into tb_sequence values(),(),(),(),(),(),(),(),();

技术图片

 

 分解步骤1:

    SELECT
        user_name,
        concat(mobile, ,) AS mobile,
        length(mobile) - length(REPLACE(mobile, ,, ‘‘)) + 1 AS size
    FROM
        qujing AS b

 

技术图片

 

 

分解步骤2:

SELECT * FROM
    tb_sequence AS a
CROSS JOIN (
    SELECT
        user_name,
        concat(mobile, ,) AS mobile,
        length(mobile) - length(REPLACE(mobile, ,, ‘‘)) + 1 AS size
    FROM
        qujing AS b
) AS b ON a.id <= b.size

技术图片

 

 

列转行sql:

SELECT
    user_name,
    REPLACE (
        substring(
            substring_index(mobile, , , a.id),
            char_length(
                substring_index(mobile, ,, a.id - 1)
            ) + 1
        ),
        ,,
        ‘‘
    ) AS mobile
FROM
    tb_sequence AS a
CROSS JOIN (
    SELECT
        user_name,
        concat(mobile, ,) AS mobile,
        length(mobile) - length(REPLACE(mobile, ,, ‘‘)) + 1 AS size
    FROM
        qujing AS b
) AS b ON a.id <= b.size

技术图片

 

 

 

 

技术图片

 

逗号分隔字段,序列化表方法实现列转行

标签:tab   sel   into   com   gif   sequence   cat   nbsp   auto   

原文地址:https://www.cnblogs.com/ooo0/p/12251621.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!