mysql列转行 行转列
列转行
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列转行 行转列的更多相关文章
- Sqlserver 列转行 行转列
sqlserver的行转列 列转行问题 行转列:1 使用Case when 方式 CREATE TABLE [StudentScores]( [UserName] NVARCHAR(20), --学生 ...
- SQL 横转竖 、竖专横(转载) 列转行 行转列
普通行列转换 问题:假设有张学生成绩表(tb)如下: 姓名 课程 分数 张三 语文 张三 数学 张三 物理 李四 语文 李四 数学 李四 物理 想变成(得到如下结果): 姓名 语文 数学 物理 --- ...
- mysql 中实现行变列
前言: mysql行列变化,最难的就是将多个列变成多行,使用的比较多的是统计学中行变列,列变行,没有找到现成的函数或者语句,所以自己写了存储过程,使用动态sql来实现,应用业务场景,用户每个月都有使用 ...
- SQL Server 动态行转列(参数化表名、分组列、行转列字段、字段值)
一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 实现代码(SQL Codes) 方法一:使用拼接SQL,静态列字段: 方法二:使用拼接SQL, ...
- SQL两列数据,行转列
SQL中只有两列数据(字段1,字段2),将其相同字段1的行转列 转换前: 转换后: --测试数据 if not object_id(N'Tempdb..#T') is null drop table ...
- MySQL、DM 行转列及字段去重(Group_Concat())
最近在使用数据库迁移适配,由MySQL 库迁移到达梦数据库,其中进行行转列时,MySQL转换达梦sql语法有些问题,特记录. 在MySQL 下有Group_Concat(expr) ,在达梦及神通数 ...
- mysql中的行转列
//查看当前商品库存 function checkProductStock($product_id){ global $wpdb; $sql="SELECT post_id,max(if(( ...
- SQL Server行转列、不确定列的行转列
本文使用的方法: 1.用Case When 2.PIVOT函数 首先,模拟一张表: -- 创建模拟数据 CREATE TABLE #TempSubjectResult ( StudentName NV ...
- [SQL]多列的行转列
create table t(name varchar(),subject varchar(),mark int) insert into t union all union all union al ...
随机推荐
- vue如何发请求
1.vue 支持开发者引入 jquery 使用 $.ajax() 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 1.首先,在 package.json 中添加 j ...
- php工作中常用到的linux命令
压缩并指定目录举例:zip -r /home/kms/kms.zip /home/kms/server/kms 解压并指定目录举例:unzip /home/kms/kms.zip -d /home/k ...
- TomCat 启动默认加载项目
在最后加上这句代码即可:<Context path="" docBase="\项目名称" reloadable="true" cros ...
- Python基础-列表、元组、字典、字符串
Python基础-列表.元组.字典.字符串 多维数组 nums1 = [1,2,3] #一维数组 nums2 = [1,2,3,[4,56]] #二维数组 nums3 = [1,2,3,4,['a ...
- GitHub and Git
book:https://git-scm.com/book/zh/v2 Git使用简易指南:https://www.bootcss.com/p/git-guide
- jsp必填项加红色星号
<th><font color=red>*</font>文字:</th>
- 洛谷P1315 [NOIP2011提高组Day2T3] 观光公交
P1315 观光公交 题目描述 风景迷人的小城Y 市,拥有n 个美丽的景点.由于慕名而来的游客越来越多,Y 市特意安排了一辆观光公交车,为游客提供更便捷的交通服务.观光公交车在第 0 分钟出现在 1号 ...
- Leetcode671.Second Minimum Node In a Binary Tree二叉树中的第二小结点
给定一个非空特殊的二叉树,每个节点都是正数,并且每个节点的子节点数量只能为 2 或 0.如果一个节点有两个子节点的话,那么这个节点的值不大于它的子节点的值. 给出这样的一个二叉树,你需要输出所有节点中 ...
- Apache配置虚拟主机,全部指向一个目录
配置虚拟主机的时候,全部指向了一个目录,解决方法是在httpd.conf中添加: NameVirtualHost *:80
- 表格存储TableStore2.0重磅发布,提供更强大数据管理能力
表格存储TableStore是阿里云自研的面向海量结构化和半结构化数据存储的Serverless NoSQL多模型数据库,被广泛用于社交.物联网.人工智能.元数据和大数据等业务场景.表格存储Table ...