列转行

SELECT
flag
,substring_index(substring_index(t.context,',', b.help_topic_id + 1), ',', -1) as result
FROM (
select 'aa' as flag,'1,2,3,4,5,6,7' as context union all
select 'bb' as flag,'1,2,3,4,5,6' as context union all
select 'cc' as flag,'1,2,3,4,5' as context union all
select 'dd' as flag,'1,2,3,4' as context union all
select 'ee' as flag,'1,2,3' as context
) t
join (
select 0 as help_topic_id union all
select 1 as help_topic_id union all
select 2 as help_topic_id union all
select 3 as help_topic_id union all
select 4 as help_topic_id union all
select 5 as help_topic_id union all
select 6 as help_topic_id union all
select 7 as help_topic_id union all
select 8 as help_topic_id union all
select 9 as help_topic_id
) b
ON b.help_topic_id < (LENGTH(t.context) - LENGTH(REPLACE(t.context, ',', '')) + 1)
order by flag,result
;

行转列使用group_concat函数即可

select
name
,group_concat(age) as ages
from (
select 'a' as name, 0 as age union all
select 'b' as name, 1 as age union all
select 'b' as name, 2 as age union all
select 'c' as name, 3 as age union all
select 'c' as name, 4 as age union all
select 'c' as name, 5 as age union all
select 'd' as name, 6 as age union all
select 'd' as name, 7 as age union all
select 'd' as name, 8 as age union all
select 'd' as name, 9 as age
) b
group by name
;

mysql列转行 行转列的更多相关文章

  1. Sqlserver 列转行 行转列

    sqlserver的行转列 列转行问题 行转列:1 使用Case when 方式 CREATE TABLE [StudentScores]( [UserName] NVARCHAR(20), --学生 ...

  2. SQL 横转竖 、竖专横(转载) 列转行 行转列

    普通行列转换 问题:假设有张学生成绩表(tb)如下: 姓名 课程 分数 张三 语文 张三 数学 张三 物理 李四 语文 李四 数学 李四 物理 想变成(得到如下结果): 姓名 语文 数学 物理 --- ...

  3. mysql 中实现行变列

    前言: mysql行列变化,最难的就是将多个列变成多行,使用的比较多的是统计学中行变列,列变行,没有找到现成的函数或者语句,所以自己写了存储过程,使用动态sql来实现,应用业务场景,用户每个月都有使用 ...

  4. SQL Server 动态行转列(参数化表名、分组列、行转列字段、字段值)

    一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 实现代码(SQL Codes) 方法一:使用拼接SQL,静态列字段: 方法二:使用拼接SQL, ...

  5. SQL两列数据,行转列

    SQL中只有两列数据(字段1,字段2),将其相同字段1的行转列 转换前: 转换后: --测试数据 if not object_id(N'Tempdb..#T') is null drop table ...

  6. MySQL、DM 行转列及字段去重(Group_Concat())

    最近在使用数据库迁移适配,由MySQL 库迁移到达梦数据库,其中进行行转列时,MySQL转换达梦sql语法有些问题,特记录. 在MySQL 下有Group_Concat(expr)  ,在达梦及神通数 ...

  7. mysql中的行转列

    //查看当前商品库存 function checkProductStock($product_id){ global $wpdb; $sql="SELECT post_id,max(if(( ...

  8. SQL Server行转列、不确定列的行转列

    本文使用的方法: 1.用Case When 2.PIVOT函数 首先,模拟一张表: -- 创建模拟数据 CREATE TABLE #TempSubjectResult ( StudentName NV ...

  9. [SQL]多列的行转列

    create table t(name varchar(),subject varchar(),mark int) insert into t union all union all union al ...

随机推荐

  1. Leetcode559.Maximum Depth of N-ary TreeN叉树的最大深度

    给定一个 N 叉树,找到其最大深度. 最大深度是指从根节点到最远叶子节点的最长路径上的节点总数. 说明: 树的深度不会超过 1000. 树的节点总不会超过 5000. class Solution { ...

  2. consul理解

    假设consul软件安装在电脑ComputerA上,那么需要注册的服务ServiceA1也需要安装在电脑ComputerA上, 一个服务就是一个提供了ip+port(或者域名)的应用程序. 服务: 服 ...

  3. ASP.NET Core修改IOC为Autofac

    如下是我为了了解如何更换ASP.NET Core中的IOC而查找的文章,如果大家英文OK的,可以直接前往阅读,同时也已经有简单的github例子供大家参考. 参考文章: ASP.NET Core文档: ...

  4. lc287 Game of Live

    lc287 Game of Live 难点在于不能使用额外的空间. 观察到数组中每一个元素要么为1要么为0,32位int只用了一位,可以利用bit操作,将第二次state存储到int变量的倒数第二位中 ...

  5. windows远程连接linux-安装xfce界面,ubuntn添加新用户

    参考:“ 使用xrdp实现windows 远程桌面连接linux”   http://blog.csdn.net/qq_33530388/article/details/64502902; http: ...

  6. vue项目 安装和配置sass & main.js引入scss文件报错

    通过npm 安装 cnpm install --save-dev sass-loader cnpm install --save-dev node-sass 在build文件夹下的webpack.ba ...

  7. php mkdir 777失败

    参考网址:https://www.cnblogs.com/52php/p/5660079.html 在linux系统中在创建文件/文件夹时有一个默认权限,此权限受 umask 设置影响,在/etc/b ...

  8. random模块&hashlib模块

    random模块1.random.randrange(1, 10):返回1-10之间的一个随机数,不包括102.random.randint(1,10):返回1-10之间的一个随机数,包括103.ra ...

  9. linux一些配置的记录

    ssh 登录设置主机的别名 这样不用写那么多东西了 在/etc/ssh/ 下的ssh_config 和 sshd_config 两个文件 前一个是配置客户端的配置文件 另一个是服务器端的配置文件 主要 ...

  10. golang之vscode环境配置

    go语言开发,选择vscode作为IDE工具也是一个不错的选择,毕竟goland收费,老是破解也挺麻烦,除了这点,不过说实话挺好用的.vscode的话相对来说就毕竟原始,适合初学者. 1.vscode ...