当前位置:网站首页>MySQL column to row conversion without Union
MySQL column to row conversion without Union
2022-06-29 04:03:00 【A bag of Yu bamboo】
Recently, I have used mysql Column turned , I found a lot of methods on the Internet, most of which are row to column methods , For column to row , Only found union This way , However, the database version of the working environment is low and does not support temporary tables , Use union The way of writing is too cumbersome , So here is a record of the use union And don't use union Column to row solution
notes : The execution efficiency of the two methods is not compared
Test data :
CREATE TABLE `test` (
`id` int NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`chinese` int DEFAULT NULL,
`math` int DEFAULT NULL,
`english` int DEFAULT NULL,
`wuli` int DEFAULT NULL,
`huaxue` int DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
INSERT INTO test.test (id, name, chinese, math, english, wuli, huaxue) VALUES(1, ' Zhang San ', 110, 120, 85, NULL, NULL);
INSERT INTO test.test (id, name, chinese, math, english, wuli, huaxue) VALUES(2, ' Li Si ', 130, 88, 89, NULL, NULL);
INSERT INTO test.test (id, name, chinese, math, english, wuli, huaxue) VALUES(3, ' Wang Wu ', 93, 124, 87, 98, 67);
Basic union Column to row writing :
-- First use union Implement column to row
select name,' Chinese language and literature ' as course, chinese as 'score' from test union
select name,' mathematics ' as course, math as 'score' from test union
select name,' English ' as course, english as 'score' from test union
select name,' Physics ' as course, wuli as 'score' from test union
select name,' chemical ' as course, huaxue as 'score' from test order by name asc
Realization effect :
Don't use union Column to row writing :
no need union The way to realize the idea :
The main core of column to row conversion is to convert one data to multiple data , If you do not query many times , You want to convert columns to rows , We need to expand the number of pieces of data according to our query requirements ; At the same time, in order to ensure that the expanded results can meet the needs of our subsequent data extraction , Therefore, it is necessary to fill the field name into the data for field extraction while expanding .
-- With the help of system tables ( Or construct temporary data by yourself ) use join Amplification data , And then use case when Implement column to row , Get the desired result set .
select
b.name,
a.COLUMN_NAME as subject ,
case a.COLUMN_NAME when 'chinese' then b.chinese when 'math' then b.math
when 'english' then b.english when 'wuli' then b.wuli when 'huaxue' then b.huaxue end as achievement
from
-- Get the field list with the help of the system table , Add custom joinid Used for data association
(select COLUMN_NAME ,'aa' as joinid from information_schema.`COLUMNS` c where TABLE_NAME ='test' and column_name not in ('id','name')) a
-- Use customization joinid join Get Cartesian product result set
left join
-- Query source table , Add custom joinid Used for data association
(select *,'aa' as joinid from test t ) b
on a.joinid=b.joinid order by b.name
Process demonstration :
Get the field name from the system table , At the same time, the user-defined Association column is added :

Use join Associated field name table and data table , Complete data construction :

Use case when Extract the desired data , Achieve the goal of column to row :

边栏推荐
- Cloud native weekly | grafana 9 was officially released; The Chinese version of cloud native vocabulary is now online
- Do you feel confused when you study at three in the morning?
- leetcode - 295. 数据流的中位数
- leetcode:304. 2D area and retrieval - matrix immutable
- 基于可变参模板实现的线程池
- [interview guide] AI algorithm interview
- 科技云报道:混合办公的B面:安全与效率如何兼得?
- Technology cloud report: side B of mixed office: how to have both security and efficiency?
- c语言 --- 分支结构
- Hcie security day41: theoretical learning: information collection and network detection
猜你喜欢

不使用union实现Mysql 列转行

百度智能云服务网格产品CSM发布 | 火热公测中

欧拉开源社区第二届理事会第二次会议召开,新华三、超聚变和龙芯中科成为理事会成员单位

Seekbar custom pictures are not displayed completely up, down, left, right / bitmaptodrawable / bitmaptodrawable inter rotation / paddingstart/paddingend /thumboffset

开发者方案 · 环境监测设备(小熊派物联网开发板)接入涂鸦IoT开发平台

科班出身,结果外包都不要

干货丨微服务架构是什么?有哪些优点和不足?

树莓派用VNC Viewer方式远程连接

自己动手搭建一个简单的网站

Why are you a test / development programmer? Can you recall
随机推荐
《运营之光3.0》全新上市——跨越时代,自我颠覆的诚意之作
Data collection and management [15]
MySQL can only add small tables when adding tables dynamically. If you increase the size of tables, you won't read data when running tasks. Is there any solution
NotImplementedError: Could not run torchvision::nms
为什么说测试岗位是巨坑?8年测试人告诉你千万别上当
Call snapstateon closed sou from Oracle CDC
Django model generates docx database design documents
Black screen and error reporting when loading custom models for gazebo with roslaunch
Devops note-05: what are the roles of Ba, SM, Po, PM, PD, dev, OPS and QA in the IT industry
Data statistical analysis (SPSS) [7]
赚钱的5个层次,你在哪一层?
Seekbar custom pictures are not displayed completely up, down, left, right / bitmaptodrawable / bitmaptodrawable inter rotation / paddingstart/paddingend /thumboffset
Set hardware breakpoint instruction for ejtag under the PMON of the Godson development board
c语言 --- 分支结构
CANoe-如何在Trace窗口解析报文并显示信息(Program Node和结构体类型系统变量的使用)
NotImplementedError: Could not run torchvision::nms
[fpga+sin] FPGA implementation of sinusoidal signal generator module based on DDS (direct digital synthesis)
87. (cesium chapter) cesium thermal map (pasted with terrain)
大神们 在富函数的open中从mysql连接池里取连接 连接池初始化是20个 如果富函数的并行度是1
二叉树序列化与反序列化(leetcode(困难))