MySQL行列转化方式
更新时间:2026年01月23日 14:30:42 作者:袅沫
文章介绍了如何将行数据转化为列数据,并展示了如何使用UNION和UNION ALL的区别,作者分享了个人经验,希望能对大家有所帮助
初始化表结构
CREATE TABLE `student_scores` ( `student_id` int NOT NULL, `student_name` varchar(50) DEFAULT NULL, `math_score` int DEFAULT NULL, `english_score` int DEFAULT NULL, `science_score` int DEFAULT NULL, PRIMARY KEY (`student_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; INSERT INTO student_scores (student_id, student_name, math_score, english_score, science_score) VALUES (1, 'Alice', 85, 90, 78), (2, 'Bob', 76, 88, 92), (3, 'Charlie', 90, 85, 80);
查询表数据:

行转化为列
由于不是我们想要的格式,我们将其转化为列式结构:
-- 行数转化为列 SELECT student_id, student_name, 'Math' AS subject, math_score AS score FROM student_scores UNION ALL SELECT student_id, student_name, 'English' AS subject, english_score AS score FROM student_scores UNION ALL SELECT student_id, student_name, 'Science' AS subject, science_score AS score FROM student_scores;
执行结果:

列转化为行
将其作为一张临时表,对其进行行列转化:
select student_id,student_name,
MIN(Case when subject = 'Math' then score end ) as math_score,
MIN(case when subject = 'English' then score end )as english_score,
MIN(case when subject = 'Science' then score end )as science_score
from (
SELECT student_id, student_name, 'Math' AS subject, math_score AS score FROM student_scores
UNION ALL
SELECT student_id, student_name, 'English' AS subject, english_score AS score FROM student_scores
UNION ALL
SELECT student_id, student_name, 'Science' AS subject, science_score AS score FROM student_scores
) AS unpivoted
GROUP BY unpivoted.student_id,unpivoted.student_name
执行结果:

扩展
union 与 union all区别
- UNION:会自动去除合并结果集中的重复记录,只返回唯一的记录。
- UNION ALL:会返回所有记录,包括重复的记录。
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
MySQL错误1449: The user specified as
在使用MySQL数据库时,有时会遇到错误1449: The user specified as a definer ('root'@'%') does not exist,本文将详细介绍这个错误的原因以及如何解决它,需要的朋友可以参考下2025-05-05
MySQL 5.6.36 Windows x64位版本的安装教程详解
这篇文章主要介绍了MySQL 5.6.36 Windows x64位版本的安装教程详解,非常不错,具有参考借鉴价值,需要的的朋友参考下吧2017-05-05
Mysql 日期时间 DATE_FORMAT(date,format)
Mysql 日期时间 DATE_FORMAT(date,format) ,需要的朋友可以参考下。2010-12-12


最新评论