列转行

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. JDBC连接整个过程

    1.导入驱动(放在lib下) connector-java-5.0.8-bin.jar 2.导入配置文件(放在src下) jdbc.properties driverClass=com.mysql.j ...

  2. 浅谈java.util.ConcurrentModificationException(并发修改异常)

    java中的list集合是我们经常使用的集合,而对集合进行增加和删除元素是我们最常用的操作.那么在什么时候对list集合什么样的操作,就会发生java.util.ConcurrentModificat ...

  3. 通过gevent实现单线程下的多socket并发

    #通过gevent实现单线程下的多socket并发 服务器 #server side import sys import socket import time import gevent from g ...

  4. poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 75541   ...

  5. python基本算法题(一)

    1.3位水仙花数计算 "3位水仙花数”是指一个三位整数,其各位数字的3次方和等于该数本身. 例如: ABC是一个“3位水仙花数”,则:A的3次方+B的3次方+C的3次方 = ABC. 使用P ...

  6. 洛谷P1315 [NOIP2011提高组Day2T3] 观光公交

    P1315 观光公交 题目描述 风景迷人的小城Y 市,拥有n 个美丽的景点.由于慕名而来的游客越来越多,Y 市特意安排了一辆观光公交车,为游客提供更便捷的交通服务.观光公交车在第 0 分钟出现在 1号 ...

  7. jquery Select2 学习笔记之中文提示 - CSDN博客

    首先学习这个东西呢,还是看官网比较全面 select2官网例子 要select2中文显示:必须要引入中文包,且一定要放在select2.js之后 [javascript] view plain cop ...

  8. 微信网页授权demo1

    要授权首先要网页域名授权 然后就index.php代码如下 <?php require_once("./function.php"); $url = 'http://'.$_ ...

  9. JetBrains PyCharm 2017.2 字体放大缩小 功能

  10. LINUX下C++编程如何获得某进程的ID

    #include <stdio.h> #include <stdlib.h> #include <unistd.h> using namespace std; pi ...