当前位置:网站首页>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 :

边栏推荐
猜你喜欢

Establishment of small and medium-sized enterprise network

88. (cesium chapter) cesium aggregation diagram

云原生周报 | Grafana 9正式发布;云原生词汇表中文版现已上线

基于xlsx的B+树索引实现

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

Do you feel confused when you study at three in the morning?

87. (cesium chapter) cesium thermal map (pasted with terrain)

【C语言】解决 “address of stack memory associated with local variable ‘num‘ returned”

【新功能】Ambire 钱包集成了 Metis 网络

百度智能云服务网格产品CSM发布 | 火热公测中
随机推荐
87. (cesium chapter) cesium thermal map (pasted with terrain)
Data statistical analysis (SPSS) [5]
泠静的想一想自己的路
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
Yangzhou needs one English IT Helpdesk Engineer -20220216
The second meeting of the Second Council of Euler open source community was held, and Xinhua III, hyperfusion and Godson Zhongke became members of the Council
自己动手搭建一个简单的网站
Go implements distributed locks
Four distributed session solutions
Qtableview gets all currently selected cells
开发者方案 · 环境监测设备(小熊派物联网开发板)接入涂鸦IoT开发平台
5-minute NLP: summary of time chronology from bag of words to transformer
基于xlsx的B+树索引实现
The great gods take connections from the MySQL connection pool in the open of the rich function. The initialization of the connection pool is 20. If the parallelism of the rich function is 1
88. (cesium chapter) cesium aggregation diagram
数据库和缓存如何保持一致性
科班出身,结果外包都不要
CDC2.2.1还不支持postgresql14.1么?基于pgbouncer连接方式下,以5433
情绪的变化需要控制
Black screen and error reporting when loading custom models for gazebo with roslaunch