当前位置:网站首页>Mysql 将逗号隔开的属性字段数据由列转行
Mysql 将逗号隔开的属性字段数据由列转行
2022-07-03 16:19:00 【小陈-程序员】
Mysql 将逗号隔开的属性字段转行为行数据
Mysql 将逗号隔开的属性字段转行为行数据。
- 场景:
在开发时,我们会根据需求进行数据库表的设计。有时我们在设计数据表时无法很好的符合三大范式,原因场景的复杂性,假如时时刻刻遵顼三大范式,会增加我们的开发量。有时候我们会在一个属性值里用逗号隔开去存储一些id值等。当在查询的时候就需要将属性转换为行。
- 数据 :
- 解决:
select a.*,SUBSTRING_INDEX(SUBSTRING_INDEX(a.content,',',b.id),',',-1) content
from temp a
join temp b
where b.id <= LENGTH(a.content)-LENGTH(REPLACE(a.content,',',''))+1
- 结果:
- 函数解析
SUBSTRING_INDEX : 获取分隔符隔开后,根据最后参数索引获取字符串 ,负数为倒数
LENGTH : 字符串 长度
REPLACE : 替换字符串
总结:
join 表需要寻找一个表属性为1开始递增的。通常会利用 mysql.help_topic 这张表,但是这张表的数据id是有限的,甚至在一些情况下没有权限访问该表。
边栏推荐
- Batch files: list all files in a directory with relative paths - batch files: list all files in a directory with relative paths
- 远程办公之大家一同实现合作编辑资料和开发文档 | 社区征文
- 特征多项式与常系数齐次线性递推
- Caching mechanism of Hibernate / session level caching mechanism
- Project -- high concurrency memory pool
- Why can't strings be directly compared with equals; Why can't some integers be directly compared with the equal sign
- App mobile terminal test [4] APK operation
- One article takes you to understand machine learning
- 1287. Elements that appear more than 25% in an ordered array
- Explore Cassandra's decentralized distributed architecture
猜你喜欢
Principles of several common IO models
App mobile terminal test [3] ADB command
"Remake Apple product UI with Android" (3) - elegant statistical chart
About text selection in web pages and counting the length of selected text
深入理解 SQL 中的 Grouping Sets 语句
深度学习之三维重建
SDNU_ ACM_ ICPC_ 2022_ Winter_ Practice_ 4th [individual]
Embedded development: seven reasons to avoid open source software
Jmeter线程组功能介绍
Explore Cassandra's decentralized distributed architecture
随机推荐
Golang 匿名函数使用
切入点表达式
Uploads labs range (with source code analysis) (under update)
Embedded development: seven reasons to avoid open source software
Nine ways to define methods in scala- Nine ways to define a method in Scala?
MB10M-ASEMI整流桥MB10M
Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (I)
无心剑中译泰戈尔《漂鸟集(1~10)》
PHP中register_globals参数设置
嵌入式开发:避免开源软件的7个理由
From the 18th line to the first line, the new story of the network security industry
Famous blackmail software stops operation and releases decryption keys. Most hospital IOT devices have security vulnerabilities | global network security hotspot on February 14
Advanced Mathematics (Seventh Edition) Tongji University exercises 2-1 personal solutions
六月 致 -.-- -..- -
Semi supervised learning
[combinatorics] combinatorial identities (review of eight combinatorial identities | product of combinatorial identities 1 | proof | use scenario | general method for finding combinatorial numbers)
First!! Is lancet hungry? Official documents
Chinese translation of Tagore's floating birds (1~10)
PHP CI(CodeIgniter)log级别设置
"Remake Apple product UI with Android" (2) -- silky Appstore card transition animation