zoukankan      html  css  js  c++  java
  • mysql中行转列与列传行的问题

    行转列:

    •   使用cross join 的方式
    •   使用case-when的方式

    列转行:

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

      以上语句实现的前提是:你需要创建一张tb_sequence序列 表 ,只有一列

    CREATE TABLE tb_sequence (
    	id INT auto_increment NOT NULL,
    	PRIMARY KEY (id)
    )
    

      

     实现的场景为:左侧的表的内容,装换为右侧

    参考说明:慕课网 mysql开发技巧(二)

  • 相关阅读:
    windows平台下一款强大的免费代理获取工具
    彻底搞懂Git Rebase
    line-height
    text-indent
    text-decoration
    text-align
    color
    CSS属性汇总
    font
    font-style
  • 原文地址:https://www.cnblogs.com/save-shengfei/p/6611803.html
Copyright © 2011-2022 走看看