首页 > 数据库 >MySQL 字段根据逗号分割实现列转行

MySQL 字段根据逗号分割实现列转行

时间:2023-09-13 14:11:06浏览次数:39  
标签:topic execute help index 转行 逗号 MySQL id name

表数据:

 期望数据:

实现sql:

select 
a.id,
b.help_topic_id +1,
a.execute_name,
#substring_index(a.execute_name,',',b.help_topic_id+1),
substring_index(substring_index(a.execute_name,',',b.help_topic_id+1),',',-1) 
from `user` a
left join mysql.help_topic b
on b.help_topic_id < (length(a.execute_name) - length(replace(a.execute_name,',',''))+1)
order by a.id, b.help_topic_id

使用到的函数:

1、SUBSTRING_INDEX(str,delim,count):

str:要处理的字符串
delim:分隔符
count:计数
正数:从左往右数,第N个分隔符的左边的全部内容,
负数,那么就是从右边开始数,第N个分隔符右边的所有内容

2、help_topic这张表是mysql自带的连续数列的表

mysql.help_topic这张表我们只用到了它的help_topic_id,
可以看到这个help_topic_id是从0开始一直连续的,
join这张表只是为了确定数据行数。
现在假设我的mysql.help_topic一共只有5条数据,那么最多可转成5行数据,若果现在字段的名字有6个就不能用mysql.help_topic这张表了。

3、substring_index(substring_index(a.execute_name,',',b.help_topic_id+1),',',-1)

select 
a.id,
b.help_topic_id +1,
a.execute_name,
#从左往右循环取 b.help_topic_id+1 个字符
substring_index(a.execute_name,',',b.help_topic_id+1), 
#从右往左第一个,右边的数据
substring_index(substring_index(a.execute_name,',',b.help_topic_id+1),',',-1) 
from `user` a
left join mysql.help_topic b
on b.help_topic_id < (length(a.execute_name) - length(replace(a.execute_name,',',''))+1)
order by a.id, b.help_topic_id

4、on条件后面的b.help_topic_id < (length(a.execute_name) - length(replace(a.execute_name,',',''))+1)

得到被逗号分隔的字段一共有几个(既计算出原始的字段长度,和去掉逗号的字段长度,两者的差就是有几个逗号,三个逗号就表示有四个字段...)

5、join 这个join最基本原理是笛卡尔积。通过这个方式来实现循环

这种方法的缺点在于,我们需要一个拥有连续数列的独立表(这里是incre_table)。并且连续数列的最大值一定要大于符合分割的值的个数。
例如有一行的mSize 有100个逗号分割的值,那么我们的incre_table 就需要有至少100个连续行。
mysql内部也有现成的连续数列表可用。如mysql.help_topic:一般能满足于大部分需求了。

参考:https://www.cnblogs.com/xcyjblog/p/15134312.html 

标签:topic,execute,help,index,转行,逗号,MySQL,id,name
From: https://www.cnblogs.com/wongzzh/p/17699569.html

相关文章

  • MySQL为什么改进LRU算法
    LRU算法概念介绍LRU(LeastRecentlyUsed,最近最少使用)算法是一种用于缓存管理的常见算法。它的核心思想是:当需要淘汰(替换)一个数据时,选择最长时间未被访问的数据进行淘汰,即选择最近最少使用的数据。以下是LRU算法的概念介绍和基本工作原理:缓存管理:LRU算法通常用于管理缓存中的数据。......
  • 完美解决MySQL ERROR:Access denied for user `root`@`localhost` (using password:YE
    windows找到mysql安装目录下的my.ini,并在最后一行添加skip-grant-tableslinux目录为etc/my.cnf在最后一行添加skip-grant-tables 1、输入mysql-uroot-p  然后回车2、输入usemysql; 3、输入updateusersetpassword=password("这里填写要设置的密码")whereuser=......
  • MySQL篇:bug1_navicat添加外键保存后不起作用(消失)
    问题在Nacicatpremium中添加外键一保存就消失用SQL语句也显示创建成功,没有报错,可是在INSERT中又起不到约束作用解决办法参考一下资料发现可能是表的类型不支持外键要在MySQL声明外键,用户应该紧记几个要点:两个表必须是InnoDB类型。在参考的表,必须有一个索引,参考的列被......
  • MySql教程____mysql架构体系
    ......
  • MySql教程(--)MySql安装-基于yum源安装mysql
    一.下载mysql的yum源 1.地址可根据自己需求下载 http://repo.mysql.com/wgethttp://repo.mysql.com/mysql57-community-release-el7-8.noarch.rpm2.执行下载源 rpm-ivhmysql57-community-release-el7-8.noarch.rpm 3.安装yum-yinstallmysql-server 4.启动servicemysqld......
  • MySQL 分组取最新的一条
    1.MySQL分组取最新的一条2.MySQLnotinnotexists CREATETABLE`test_dept`(`deptid`int(11)NOTNULL,`deptname`varchar(255)CHARACTERSETutf8COLLATEutf8_general_ciDEFAULTNULL,PRIMARYKEY(`deptid`)USINGBTREE)ENGINE=InnoDBCHARACTERSET......
  • MySQL 8.0安装 Windows
    方案概述本方案提供winserver下mysql两种安装方式:图像化安装:MySQLInstallerMSIZIPArchive压缩包(命令行)方式实施步骤1.图像化安装:MySQLInstallerMSI官方下载地址:https://dev.mysql.com/downloads/这里建议选择:community版本通过离线方式安装,web-community版本需要链接网络下载......
  • MySql教程(--)mysql系统常用命令集
    --mysql连接1.连接到本机上的MYSQL 命令:mysql-u[username]-p mysql-uroot-p,回车后提示你输密码。2.连接到远程主机上的MYSQLhost:远程主机地址username:用户名password:密码 命令:mysql-h[host]-u[username]-p[password] mysql-h192.168.10.153-uroot......
  • nacos redis mysql 服务启动脚本
    nacos#cat/etc/systemd/system/nacos.service[Unit]Description=nacosAfter=network.target[Service]Type=forkingExecStart=/usr/local/src/nacos/bin/startup.shExecStop=/usr/local/src/nacos/bin/shutdown.shPrivateTmp=true[Install]WantedBy=multi-user.......
  • 【Docker】# MySQL从安装到备份还原
    Writer:夏明亮Date:2022/03/28Docker安装略MySQL安装部署https://hub.docker.com/_/mysql/#:~:text=Run%20docker%20stack%20deploy%20-c%20stack.yml%20mysql%20%28or,you%20to%20run%20commands%20inside%20a%20Docker%20container.搜索合适的镜像[root@docker-master~]#dockers......