MySQL查询的横竖转换
横转竖
-- 创建表
CREATE TABLE `student` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL DEFAULT '',
`yuwen` smallint(3) unsigned NOT NULL DEFAULT '0',
`yingyu` smallint(3) unsigned NOT NULL DEFAULT '0',
`math` smallint(3) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- 插入数据
INSERT INTO `student`(`id`, `name`, `yuwen`, `yingyu`, `math`) VALUES (0, 'non', 60, 59, 80);
INSERT INTO `student`(`id`, `name`, `yuwen`, `yingyu`, `math`) VALUES (0, 'robin', 80, 80, 40);
源数据
mysql> select * from student;
+----+-------+-------+--------+------+
| id | name | yuwen | yingyu | math |
+----+-------+-------+--------+------+
| 1 | non | 60 | 59 | 80 |
| 2 | robin | 80 | 80 | 40 |
+----+-------+-------+--------+------+
2 rows in set (0.02 sec)
横转竖
mysql> select * from (
select `name` as `name`,`yuwen` as `subject`,`yuwen` as result from student
UNION ALL
select `name` as `name`,`yingyu` as `subject`,`yingyu` as result from student
UNION ALL
select `name` as `name`,`math` as `subject`,`math` as result from student
) t ORDER BY name;
+-------+---------+--------+
| name | subject | result |
+-------+---------+--------+
| non | 59 | 59 |
| non | 60 | 60 |
| non | 80 | 80 |
| robin | 40 | 40 |
| robin | 80 | 80 |
| robin | 80 | 80 |
+-------+---------+--------+
6 rows in set (0.03 sec)
竖转横
创建表
CREATE TABLE `student1` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL DEFAULT '',
`subject` varchar(255) NOT NULL DEFAULT '',
`result` smallint(3) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
插入数据
INSERT INTO `student1`(`id`, `name`, `subject`, `result`) VALUES (0, 'non', 'yuwen', 60);
INSERT INTO `student1`(`id`, `name`, `subject`, `result`) VALUES (0, 'non', 'yingyu', 59);
INSERT INTO `student1`(`id`, `name`, `subject`, `result`) VALUES (0, 'non', 'math', 80);
INSERT INTO `student1`(`id`, `name`, `subject`, `result`) VALUES (0, 'robin', 'yuwen', 80);
INSERT INTO `student1`(`id`, `name`, `subject`, `result`) VALUES (0, 'robin', 'yingyu', 80);
INSERT INTO `student1`(`id`, `name`, `subject`, `result`) VALUES (0, 'robin', 'math', 40);
源数据
mysql> select * from student1;
+----+-------+---------+--------+
| id | name | subject | result |
+----+-------+---------+--------+
| 1 | non | yuwen | 60 |
| 2 | non | yingyu | 59 |
| 3 | non | math | 80 |
| 4 | robin | yuwen | 80 |
| 5 | robin | yingyu | 80 |
| 6 | robin | math | 40 |
+----+-------+---------+--------+
6 rows in set (0.04 sec)
竖转横
mysql> select name as name, max(case subject when 'yuwen' then result else 0 end) as 'yuwen',
-> max(case subject when 'yingyu' then result else 0 end) as 'yingyu',
-> max(case subject when 'math' then result else 0 end) as 'math'
-> from student1 group by name;
+-------+-------+--------+------+
| name | yuwen | yingyu | math |
+-------+-------+--------+------+
| non | 60 | 59 | 80 |
| robin | 80 | 80 | 40 |
+-------+-------+--------+------+
2 rows in set (0.07 sec)
本文作者:nonsenseLive
本文链接:https://www.cnblogs.com/venchi/articles/14928664.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步