当前位置:网站首页> 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字段分割内容请搜索软件开发网以前的文章或继续浏览下面的相关文章希望大家以后多多支持软件开发网!
边栏推荐
- UE4蓝图学习篇(四)--流程控制ForLoop和WhileLoop
- 2014阿里巴巴web前实习生项目分析(1)
- 【全网首发】Redis系列3:高可用之主从架构的
- [unity] upgraded version · Excel data analysis, automatically create corresponding C classes, automatically create scriptableobject generation classes, and automatically serialize asset files
- Slide the uniapp to a certain height and fix an element to the top effect demo (organize)
- pytorch_ Yolox pruning [with code]
- Method of canceling automatic watermarking of uploaded pictures by CSDN
- Volatile keyword
- OpenSSL:适用TLS与SSL协议的全功能工具包,通用加密库
- Redis persistence mechanism
猜你喜欢

【LeetCode】19、 删除链表的倒数第 N 个结点

Sword finger offer question brushing record 1

Aardio - does not declare the method of directly passing float values

Advantages of link local address in IPv6

Dayu200 experience officer runs the intelligent drying system page based on arkui ETS on dayu200

Cloud native technology container knowledge points

MySQL authentication bypass vulnerability (cve-2012-2122)
![[leetcode] 19. Delete the penultimate node of the linked list](/img/ab/25cb6d6538ad02d78f7d64b2a2df3f.png)
[leetcode] 19. Delete the penultimate node of the linked list

How to choose indoor LED display? These five considerations must be taken into account

Adavit -- dynamic network with adaptive selection of computing structure
随机推荐
Comparison between variable and "zero value"
Volatile keyword
Method of canceling automatic watermarking of uploaded pictures by CSDN
On the problems of born charge and non analytical correction in phonon and heat transport calculations
Project duplicate template
uniapp滑动到一定的高度后固定某个元素到顶部效果demo(整理)
Dayu200 experience officer runs the intelligent drying system page based on arkui ETS on dayu200
Typescript get function parameter type
QT信号和槽
How to achieve text animation effect
2022-07-05 use TPCC to conduct sub query test on stonedb
UE4蓝图学习篇(四)--流程控制ForLoop和WhileLoop
【全网首发】Redis系列3:高可用之主从架构的
[untitled]
【编译原理】做了一半的LR(0)分析器
How to confirm the storage mode of the current system by program?
【踩坑合辑】Attempting to deserialize object on CUDA device+buff/cache占用过高+pad_sequence
【Unity】升级版·Excel数据解析,自动创建对应C#类,自动创建ScriptableObject生成类,自动序列化Asset文件
Interview question: AOF rewriting mechanism, redis interview must ask!!!
(18) LCD1602 experiment