当前位置:网站首页> 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字段分割内容请搜索软件开发网以前的文章或继续浏览下面的相关文章希望大家以后多多支持软件开发网!
边栏推荐
- 金融人士必读书籍系列之六:权益投资(基于cfa考试内容大纲和框架)
- Puppeteer连接已有Chrome浏览器
- 企業不想換掉用了十年的老系統
- View
- 企业不想换掉用了十年的老系统
- 机试刷题1
- Balanced Multimodal Learning via On-the-fly Gradient Modulation(CVPR2022 oral)
- 让我们,从头到尾,通透网络I/O模型
- #DAYU200体验官# 在DAYU200运行基于ArkUI-eTS的智能晾晒系统页面
- Dayu200 experience officer runs the intelligent drying system page based on arkui ETS on dayu200
猜你喜欢
![[compilation principle] LR (0) analyzer half done](/img/ec/b13913b5d5c5a63980293f219639a4.png)
[compilation principle] LR (0) analyzer half done

Rust knowledge mind map XMIND

ACL 2022 | small sample ner of sequence annotation: dual tower Bert model integrating tag semantics

#DAYU200体验官# 在DAYU200运行基于ArkUI-eTS的智能晾晒系统页面

Improving Multimodal Accuracy Through Modality Pre-training and Attention

Balanced Multimodal Learning via On-the-fly Gradient Modulation(CVPR2022 oral)

Financial professionals must read book series 6: equity investment (based on the outline and framework of the CFA exam)

自定义 swap 函数

企业不想换掉用了十年的老系统

云原生技术--- 容器知识点
随机推荐
Designed for decision tree, the National University of Singapore and Tsinghua University jointly proposed a fast and safe federal learning system
Detailed explanation of ThreadLocal
BasicVSR_PlusPlus-master测试视频、图片
动作捕捉用于蛇运动分析及蛇形机器人开发
On the problems of born charge and non analytical correction in phonon and heat transport calculations
Signed and unsigned keywords
2022-07-05 use TPCC to conduct sub query test on stonedb
ICLR 2022 | pre training language model based on anti self attention mechanism
Custom swap function
Pit encountered by handwritten ABA
Windows auzre background operation interface of Microsoft's cloud computing products
Balanced Multimodal Learning via On-the-fly Gradient Modulation(CVPR2022 oral)
项目复盘模板
【编译原理】做了一半的LR(0)分析器
MySQL教程的天花板,收藏好,慢慢看
#DAYU200体验官# 首页aito视频&Canvas绘制仪表盘(ets)
浅谈网络安全之文件上传
TypeScript获取函数参数类型
The ceiling of MySQL tutorial. Collect it and take your time
rust知识思维导图xmind