当前位置:网站首页> 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字段分割内容请搜索软件开发网以前的文章或继续浏览下面的相关文章希望大家以后多多支持软件开发网!
边栏推荐
- Slide the uniapp to a certain height and fix an element to the top effect demo (organize)
- TypeScript获取函数参数类型
- Aardio - construct a multi button component with customplus library +plus
- [unity] upgraded version · Excel data analysis, automatically create corresponding C classes, automatically create scriptableobject generation classes, and automatically serialize asset files
- What are the specific steps and schedule of IELTS speaking?
- HDU 5077 NAND (violent tabulation)
- Self made j-flash burning tool -- QT calls jlinkarm DLL mode
- Pit encountered by handwritten ABA
- Const keyword
- OpenSSL:适用TLS与SSL协议的全功能工具包,通用加密库
猜你喜欢
MySQL ---- first acquaintance with MySQL
Advantages of link local address in IPv6
Config:invalid signature solution and troubleshooting details
config:invalid signature 解决办法和问题排查详解
Adavit -- dynamic network with adaptive selection of computing structure
[unity] upgraded version · Excel data analysis, automatically create corresponding C classes, automatically create scriptableobject generation classes, and automatically serialize asset files
儿童睡衣(澳大利亚)AS/NZS 1249:2014办理流程
[compilation principle] LR (0) analyzer half done
CocosCreator+TypeScripts自己写一个对象池
Custom swap function
随机推荐
Traversal of a tree in first order, middle order, and then order
The statement that allows full table scanning does not seem to take effect set odps sql. allow. fullscan=true; I
Aardio - does not declare the method of directly passing float values
leetcode:面试题 17.24. 子矩阵最大累加和(待研究)
Introduction to network basics
Pit encountered by handwritten ABA
European Bioinformatics Institute 2021 highlights report released: nearly 1million proteins have been predicted by alphafold
OpenSSL:适用TLS与SSL协议的全功能工具包,通用加密库
BasicVSR_ Plusplus master test videos and pictures
poj 1094 Sorting It All Out (拓扑排序)
Void keyword
Plafond du tutoriel MySQL, bien collecté, regardez lentement
Les entreprises ne veulent pas remplacer un système vieux de dix ans
View
浅谈网络安全之文件上传
Rust knowledge mind map XMIND
企业不想换掉用了十年的老系统
企業不想換掉用了十年的老系統
如何实现文字动画效果
服务器的系统怎么选者