当前位置:网站首页> MySQL实现字段分割一行转多行的示例代码
MySQL实现字段分割一行转多行的示例代码
2022-07-06 15:36:00 【1024问】
先看一下数据结构,我这里字段比较少,只弄了最重要的部分
根据我们上次学到的LEFT()函数进行分组
SELECT LEFT(provinces,6),COUNT(1) FROM `region_map_copy` GROUP BY LEFT(provinces,6)
得到的结果如下:
这样的效果并不是我们想要的,我们是要210000所有的都合并起来
使用SUBSTRING_INDEX(),LEFT()这两个函数,在加help_topic这个表
help_topic:以字符拆分,一行转多行
SELECT COUNT(*), SUBSTRING_INDEX( SUBSTRING_INDEX( a.provinces, ',', b.help_topic_id + 1 ), ',',- 1 ) AS ids FROM `region_map_copy` AS a JOIN mysql.help_topic AS b ON b.help_topic_id < ( LENGTH( a.provinces ) - LENGTH( REPLACE ( a.provinces, ',', '' ) ) + 1 ) GROUP BY (SUBSTRING_INDEX( SUBSTRING_INDEX( a.provinces, ',', b.help_topic_id + 1 ), ',',- 1 ) )
结果如下:
得到了我们想要的
mysql 字段截取函数:
left(字段名,index) 从左边开始第index开始截取
right(字段名,index)从右边开始第index开始截取
substring(字段名,index)当index>0从左边开始截取直到结束 当index<0从右边开始截取直到结束 当index=0返回空
substring(字段名,index,len)从index开始,截取len长度
substring_index(字段名,str,count),str是截取的字段 count是从哪里开始截取(0从左边第0个开始,-1从右边第一个开始)
注意:这种方式不支持mariadb
补充:下面看下MySql数据库表中字段用逗号分隔,字段进行条件查询
select p.* from t_project_info p
select p.*from t_project_info pwhere FIND_IN_SET(6,p.thematic_library_ids)
到此这篇关于MySQL实现字段分割(一行转多行)的文章就介绍到这了,更多相关mysql字段分割内容请搜索软件开发网以前的文章或继续浏览下面的相关文章希望大家以后多多支持软件开发网!
边栏推荐
- #DAYU200体验官# 在DAYU200运行基于ArkUI-eTS的智能晾晒系统页面
- CocosCreator+TypeScripts自己写一个对象池
- Let's see through the network i/o model from beginning to end
- 专为决策树打造,新加坡国立大学&清华大学联合提出快速安全的联邦学习新系统
- Improving Multimodal Accuracy Through Modality Pre-training and Attention
- Machine test question 1
- 【全网首发】Redis系列3:高可用之主从架构的
- 允许全表扫描 那个语句好像不生效set odps.sql.allow.fullscan=true;我
- NPM cannot install sharp
- Config:invalid signature solution and troubleshooting details
猜你喜欢
CSDN 上传图片取消自动加水印的方法
【编译原理】做了一半的LR(0)分析器
视图(view)
pytorch_ Yolox pruning [with code]
Thinkphp5 multi table associative query method join queries two database tables, and the query results are spliced and returned
Leetcode exercise - Sword finger offer 26 Substructure of tree
Matlab tips (27) grey prediction
Let's see through the network i/o model from beginning to end
监控界的最强王者,没有之一!
C three ways to realize socket data reception
随机推荐
【Unity】升级版·Excel数据解析,自动创建对应C#类,自动创建ScriptableObject生成类,自动序列化Asset文件
General implementation and encapsulation of go diversified timing tasks
Windows auzre background operation interface of Microsoft's cloud computing products
rust知识思维导图xmind
树的先序中序后序遍历
Redis 持久化机制
Thinkphp5 multi table associative query method join queries two database tables, and the query results are spliced and returned
MATLAB小技巧(27)灰色预测
European Bioinformatics Institute 2021 highlights report released: nearly 1million proteins have been predicted by alphafold
Unified Focal loss: Generalising Dice and cross entropy-based losses to handle class imbalanced medi
Leetcode exercise - Sword finger offer 26 Substructure of tree
项目复盘模板
Balanced Multimodal Learning via On-the-fly Gradient Modulation(CVPR2022 oral)
What are the specific steps and schedule of IELTS speaking?
UE4 blueprint learning chapter (IV) -- process control forloop and whileloop
允许全表扫描 那个语句好像不生效set odps.sql.allow.fullscan=true;我
UVa 11732 – strcmp() Anyone?
如何实现文字动画效果
poj 1094 Sorting It All Out (拓扑排序)
Traversal of a tree in first order, middle order, and then order