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 ...
随机推荐
- 字符界面总是显示 login incorrect
一般来说出现这样的提示,是因为登陆的密码错误,如果密码中有数字,最好用主键盘输入,用数字键盘会有错误.
- Eclipse luna安装 Ivy
在线安装地址:http://www.apache.org/dist/ant/ivyde/updatesite
- 用CSS添加选中文字的背景色
- Socket与TcpClient的区别
原文:Socket与TcpClient的区别 Socket和TcpClient有什么区别 原文:http://wxwinter.spaces.live.com/blog/cns!C36588978AF ...
- python 数据库风格的DataFrame合并
- 稳定性专题 | StackOverFlowError 常见原因及解决方法
导读 『StabilityGuide』是阿里多位阿里技术工程师共同发起的稳定性领域的知识库开源项目,涵盖性能压测.故障演练.JVM.应用容器.服务框架.流量调度.监控.诊断等多个技术领域,以更结构化的 ...
- Django项目:CRM(客户关系管理系统)--07--03PerfectCRM创建基本数据02
from django.conf.urls import url from DBadd import auth_views from DBadd import crm_views urlpattern ...
- linux 查看磁盘空间占用情况
工作中有时被分配的测试机空间不大,经常遇到磁盘空间占满的情况.排查过程如下: 一.首先使用df -h 命令查看磁盘剩余空间,通过以下图看出/目录下的磁盘空间已经被占满. 二.进入根目录,因为最近常用的 ...
- LintCode_372 在O(1)时间复杂度删除链表节点
题目 给定一个单链表中的表头和一个等待被删除的节点(非表头或表尾).请在在O(1)时间复杂度删除该链表节点.并在删除该节点后,返回表头. 思路 因为O(1)内删除所以 不能从头节点去遍历找他的前驱节点 ...
- Django学习之路由分发和反向解析
原 Django学习之路由分发和反向解析 2018年07月12日 14:04:55 huangql517 阅读数 519 1>路由分发 我们之前学习的路由配置都是在项目的全局控制文件(项目名称目 ...