当前位置:网站首页> 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字段分割内容请搜索软件开发网以前的文章或继续浏览下面的相关文章希望大家以后多多支持软件开发网!
边栏推荐
- Improving Multimodal Accuracy Through Modality Pre-training and Attention
- How big is the empty structure?
- Aardio - integrate variable values into a string of text through variable names
- 使用云服务器搭建代理
- 金融人士必读书籍系列之六:权益投资(基于cfa考试内容大纲和框架)
- Export MySQL table data in pure mode
- Comparison between variable and "zero value"
- View
- 允许全表扫描 那个语句好像不生效set odps.sql.allow.fullscan=true;我
- UDP programming
猜你喜欢

Clip +json parsing converts the sound in the video into text

【Unity】升级版·Excel数据解析,自动创建对应C#类,自动创建ScriptableObject生成类,自动序列化Asset文件

Unified Focal loss: Generalising Dice and cross entropy-based losses to handle class imbalanced medi

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

让我们,从头到尾,通透网络I/O模型

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

Thinkphp5 multi table associative query method join queries two database tables, and the query results are spliced and returned

Method of canceling automatic watermarking of uploaded pictures by CSDN

Sword finger offer question brushing record 1

Export MySQL table data in pure mode
随机推荐
Financial professionals must read book series 6: equity investment (based on the outline and framework of the CFA exam)
Unified Focal loss: Generalising Dice and cross entropy-based losses to handle class imbalanced medi
CRMEB商城系统如何助力营销?
CocosCreator+TypeScripts自己写一个对象池
AdaViT——自适应选择计算结构的动态网络
uniapp滑动到一定的高度后固定某个元素到顶部效果demo(整理)
Improving Multimodal Accuracy Through Modality Pre-training and Attention
On the problems of born charge and non analytical correction in phonon and heat transport calculations
#DAYU200体验官# 在DAYU200运行基于ArkUI-eTS的智能晾晒系统页面
Mysql 身份认证绕过漏洞(CVE-2012-2122)
#DAYU200体验官# 首页aito视频&Canvas绘制仪表盘(ets)
云原生技术--- 容器知识点
European Bioinformatics Institute 2021 highlights report released: nearly 1million proteins have been predicted by alphafold
Some suggestions for foreign lead2022 in the second half of the year
Project duplicate template
「小程序容器技术」,是噱头还是新风口?
Balanced Multimodal Learning via On-the-fly Gradient Modulation(CVPR2022 oral)
Clip +json parsing converts the sound in the video into text
UDP programming
The difference between enumeration and define macro