当前位置:网站首页>[MySQL project practical optimization] convert multiple rows of data into the same row and multiple columns for display
[MySQL project practical optimization] convert multiple rows of data into the same row and multiple columns for display
2022-07-18 19:55:00 【InfoQ】
Business scenario

Skills used in business
- The left connection gets the data in the remaining tables
caseConvert status data into text descriptions , Be similar to1 -> Enable , 0 -> Ban
- Table multi-level nested query
- Table data grouping query +
group_concatThe connection target field is merged into 1 In records
substring_index, Intercept string according to separator
replaceReplace string data
From inside to outside, query and process as a temporary table
Prepare basic data for joint table query
bSELECT
c.mdm_code cCode,
c.mdm_name cName,
a.mdm_name aName,
( CASE a.mdm_splicing_type WHEN 'B_01' THEN ' Only the material description is displayed ' WHEN 'B_02' THEN ' Both material description and material abbreviation are displayed ' ELSE ' Neither material description nor material abbreviation is displayed ' END ) aType,
a.mdm_required aRequired ,
ap.mdm_des apDes,
np.mdm_des npDes,
sp.mdm_des spDes
FROM
attribute_1 a
LEFT JOIN mdm_classification c ON a.mdm_classifi_note_id = c.id
LEFT JOIN mdm_abbr_spl ap on ap.mdm_product_code2 = left(c.mdm_code,4)
LEFT JOIN mdm_note_spl np on np.mdm_product_code2 = left(c.mdm_code,4)
LEFT JOIN mdm_spemod_spl sp on sp.mdm_product_code3 = c.mdm_code
WHERE
c.mdm_code IS NOT NULL
) b

A temporary table to code Group and merge data
bcodegroup_concatcSELECT
cCode,
cName,
CONCAT('_',GROUP_CONCAT( aName SEPARATOR ' ' )) aNames,
CONCAT('_',GROUP_CONCAT( aType SEPARATOR ' ' )) aTypes,
GROUP_CONCAT( apDes SEPARATOR ' ' ) apDes,
GROUP_CONCAT( npDes SEPARATOR ' ' ) npDes,
GROUP_CONCAT( spDes SEPARATOR ' ' ) spDes
FROM
temp_b
code
Split data into multiple columns
c
REPLACE + SUBSTRING_INDEXSELECT
cCode ' Three level classification code ',
cName ' The name of the third level classification ',
REPLACE(SUBSTRING_INDEX(aNames,' ',1),'_','') ' attribute 1',
REPLACE(SUBSTRING_INDEX(aTypes,' ',1),'_','') ' Limit 1',
REPLACE(SUBSTRING_INDEX(aNames,' ',2),SUBSTRING_INDEX(aNames,' ',1),'') ' attribute 2',
REPLACE(SUBSTRING_INDEX(aTypes,' ',2),SUBSTRING_INDEX(aTypes,' ',1),'') ' Limit 2',
REPLACE(SUBSTRING_INDEX(aNames,' ',3),SUBSTRING_INDEX(aNames,' ',2),'') ' attribute 3',
REPLACE(SUBSTRING_INDEX(aTypes,' ',3),SUBSTRING_INDEX(aTypes,' ',2),'') ' Limit 3'
FROM
temp_c

SUBSTRING_INDEX function
SUBSTRING_INDEX(str, delim, count)countstrcountdelimnstrnstr = aa_bb_cc_dd
substring_index(str, '_', 1) = aa
substring_index(str, '_', 2) = aa_bb
substring_index(str, '_', 3) = aa_bb_cc
边栏推荐
- 内存函数的介绍及模拟实现
- airtest+poco多脚本、多设备批处理运行测试用例自动生成测试报告
- Unity 视频控制暂停播放以及滑动条拖拽(笔记)
- 射频芯片ATE测试从入门到放弃之收发机
- 公司股权结构设计的原则与因素
- Airtest+poco multi script, multi device batch run test cases automatically generate test reports
- 面试微服务
- Activity--startActivityForResult()-返回数据给上一个活动方法记录
- Day 4 training
- Go language integer bisection template
猜你喜欢

1.线程与进程

Vivado ROM IP核

【OpenCV 例程200篇】232. 特征描述之頻譜方法

Use of unity dotween plug-in and itween plug-in (notes)

面试微服务

Orm déchiqueté à la main (générique + annotation + réflexion)

虚拟化架构

基于FPGA的内部IP核fifo信号仿真

Horizon 8 测试环境部署(6): UAG 负载均衡配置-2

Dark horse programmer - software testing -14 stage 3- function testing -66-77 Zen introduction, product manager uses Zen, super administrator uses Zen, super administrator modifies security strategy,
随机推荐
Common functions of JMeter - parametric introduction
BUCK 电路PSIM仿真模型搭建之三 (数字化差分方程的仿真)
电力系统经济调度(Matlab完整代码实现)
Logs issue 2022/07/16 | Huawei Noah laboratory Zhou Min: graph data modeling and analysis from the perspective of curvature
cad中格式化输出文本
Towards verifiable AI: five challenges of formal methods
solidity的代码
Mental Poker Revisited学习笔记
leetcode 43. 字符串相乘 (字符串+模拟)
Singleton mode in QT: implement a singleton interface class
公司股权结构设计的原则与因素
SI24R2E_ Smart electronic student card 2.4GHz attendance scheme chip
【MySql项目实战优化】多行数据转化为同一行多列显示
5.线程分离
3.终止线程
Behavioral model summary
Top level design scheme of the company's equity structure (case)
Machine learning: cross entropy from theory to code
六、關聯查詢優化、七 排序分組優化、八截取查詢分析
[200 opencv routines] 232 Spectral method of feature description