mysql 列转行的技巧(分享)
作者:jingxian 时间:2024-01-18 09:54:13
前言:
由于很多业务表因为历史原因或者性能原因,都使用了违反第一范式的设计模式。即同一个列中存储了多个属性值(具体结构见下表)。
这种模式下,应用常常需要将这个列依据分隔符进行分割,并得到列转行的结果。
表数据:
ID | Value |
1 | tiny,small,big |
2 | small,medium |
3 | tiny,big |
期望得到结果:
ID | Value |
1 | tiny |
1 | small |
1 | big |
2 | small |
2 | medium |
3 | tiny |
3 | big |
正文:
#需要处理的表
create table tbl_name (ID int ,mSize varchar(100));
insert into tbl_name values (1,'tiny,small,big');
insert into tbl_name values (2,'small,medium');
insert into tbl_name values (3,'tiny,big');
#用于循环的自增表
create table incre_table (AutoIncreID int);
insert into incre_table values (1);
insert into incre_table values (2);
insert into incre_table values (3);
select a.ID,substring_index(substring_index(a.mSize,',',b.AutoIncreID),',',-1)
from
tbl_name a
join
incre_table b
on b.AutoIncreID <= (length(a.mSize) - length(replace(a.mSize,',',''))+1)
order by a.ID;
原理分析:
这个join最基本原理是笛卡尔积。通过这个方式来实现循环。
以下是具体问题分析:
length(a.Size) - length(replace(a.mSize,',',''))+1 表示了,按照逗号分割后,改列拥有的数值数量,下面简称n
join过程的伪代码:
根据ID进行循环
{
判断:i 是否 <= n
{
获取最靠近第 i 个逗号之前的数据, 即 substring_index(substring_index(a.mSize,',',b.ID),',',-1)
i = i +1
}
ID = ID +1
}
总结:
这种方法的缺点在于,我们需要一个拥有连续数列的独立表(这里是incre_table)。并且连续数列的最大值一定要大于符合分割的值的个数。
例如有一行的mSize 有100个逗号分割的值,那么我们的incre_table 就需要有至少100个连续行。
当然,mysql内部也有现成的连续数列表可用。如mysql.help_topic: help_topic_id 共有504个数值,一般能满足于大部分需求了。
改写后如下:
select a.ID,substring_index(substring_index(a.mSize,',',b.help_topic_id+1),',',-1)
from
tbl_name a
join
mysql.help_topic b
on b.help_topic_id < (length(a.mSize) - length(replace(a.mSize,',',''))+1)
order by a.ID;
标签:mysql,列转行
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
基于Token的身份验证的方法
2023-09-27 19:05:41
JS getRandomValues和Math.random方法深入解析
2024-04-17 09:54:42
python匿名函数lambda原理及实例解析
2023-01-05 02:55:07
Random 在 Python 中的使用方法
2022-04-29 14:21:37
![](https://img.aspxhome.com/file/2023/3/131093_0s.png)
用javascript做拖动层布局的思路
2008-05-30 13:38:00
vue实现下拉菜单树
2024-05-09 15:18:39
![](https://img.aspxhome.com/file/2023/7/126477_0s.jpg)
Vue项目之学生管理系统实例详解
2024-04-28 09:31:57
![](https://img.aspxhome.com/file/2023/5/132895_0s.png)
如何利用JSHint减少JavaScript的错误
2024-05-28 15:37:40
![](https://img.aspxhome.com/file/2023/2/123432_0s.png)
Python3如何将源目录中的图片用MD5命名并可以设定目标目录
2023-11-28 10:33:17
Mysql的Table doesn't exist问题及解决
2024-01-16 05:03:13
PHP结合vue导出excel出现乱码的解决方法分享
2023-05-30 09:18:25
![](https://img.aspxhome.com/file/2023/5/55315_0s.png)
python网络编程学习笔记(三):socket网络服务器
2023-01-11 10:40:29
Python数据分析:手把手教你用Pandas生成可视化图表的教程
2023-01-29 21:18:50
![](https://img.aspxhome.com/file/2023/8/68338_0s.jpg)
virtualenv实现多个版本Python共存
2021-11-27 00:50:44
![](https://img.aspxhome.com/file/2023/6/64706_0s.png)
PHP屏蔽蜘蛛访问代码及常用搜索引擎的HTTP_USER_AGENT
2023-09-08 08:07:37
![](https://img.aspxhome.com/file/2023/8/55628_0s.jpg)
JavaScript的私有成员
2009-03-25 20:45:00
浅谈pytorch、cuda、python的版本对齐问题
2022-12-03 13:04:54
mysql启动报错MySQL server PID file could not be found
2024-01-22 18:41:59
![](https://img.aspxhome.com/file/2023/7/106197_0s.jpg)
Python中的装饰器使用
2023-05-25 13:14:09
Python使用MapReduce编程模型统计销量
2021-07-16 14:24:43
![](https://img.aspxhome.com/file/2023/8/102788_0s.png)