|
场景1 行转换列
1、表结构和数据- /*
- Navicat Premium Data Transfer
- Source Server : 本地
- Source Server Type : MySQL
- Source Server Version : 80027
- Source Host : localhost:3306
- Source Schema : school
- Target Server Type : MySQL
- Target Server Version : 80027
- File Encoding : 65001
- Date: 13/06/2024 14:50:51
- */
- SET NAMES utf8mb4;
- SET FOREIGN_KEY_CHECKS = 0;
- -- ----------------------------
- -- Table structure for score
- -- ----------------------------
- DROP TABLE IF EXISTS `score`;
- CREATE TABLE `score` (
- `stu_no` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '学号',
- `course_no` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '课程号',
- `score_prize` decimal(4, 1) NULL DEFAULT NULL COMMENT '成绩',
- PRIMARY KEY (`stu_no`, `course_no`) USING BTREE
- ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
- -- ----------------------------
- -- Records of score
- -- ----------------------------
- INSERT INTO `score` VALUES ('0001', '0001', 40.0);
- INSERT INTO `score` VALUES ('0001', '0002', 50.0);
- INSERT INTO `score` VALUES ('0001', '0003', 48.0);
- INSERT INTO `score` VALUES ('0002', '0001', 40.0);
- INSERT INTO `score` VALUES ('0002', '0002', 30.0);
- INSERT INTO `score` VALUES ('0002', '0003', 99.0);
- INSERT INTO `score` VALUES ('0003', '0001', 70.0);
- INSERT INTO `score` VALUES ('0003', '0002', 77.0);
- INSERT INTO `score` VALUES ('0003', '0003', 60.0);
- SET FOREIGN_KEY_CHECKS = 1;
复制代码 2、效果图说明,第一列用户信息stu_no,第二列课程号course_no,第三列课程成绩
stu_no语文数学英语000140.050.048.0000240.030.099.0000370.077.060.03、实现SQL- select stu_no,
- sum(IF(course_no = '0001', score_prize, 0)) as '语文',
- sum(IF(course_no = '0002', score_prize, 0)) as '数学',
- sum(IF(course_no = '0003', score_prize, 0)) as '英语'
- from score
- group by stu_no;
复制代码 场景2:列转换行
1、准备数据表结构和数据- /*
- Navicat Premium Data Transfer
- Source Server : 本地
- Source Server Type : MySQL
- Source Server Version : 80027
- Source Host : localhost:3306
- Source Schema : school
- Target Server Type : MySQL
- Target Server Version : 80027
- File Encoding : 65001
- Date: 13/06/2024 14:54:37
- */
- SET NAMES utf8mb4;
- SET FOREIGN_KEY_CHECKS = 0;
- -- ----------------------------
- -- Table structure for cjs
- -- ----------------------------
- DROP TABLE IF EXISTS `cjs`;
- CREATE TABLE `cjs` (
- `name` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
- `chinese` int NULL DEFAULT NULL,
- `math` int NULL DEFAULT NULL,
- `phy` int NULL DEFAULT NULL
- ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
- -- ----------------------------
- -- Records of cjs
- -- ----------------------------
- INSERT INTO `cjs` VALUES ('张三', 89, 90, 79);
- INSERT INTO `cjs` VALUES ('李四', 88, 79, 90);
- SET FOREIGN_KEY_CHECKS = 1;
复制代码 2、效果图
namecourse张三90张三89张三79李四79李四88李四903、业务代码- select *
- from (
- select name, math as course
- from cjs
- union all
- select name, chinese as course
- from cjs
- union all
- select name, phy as course
- from cjs
- ) t
- order by t.name;
复制代码 到此这篇关于MySQL行列互换的实现示例的文章就介绍到这了,更多相关MySQL行列互换内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
来源:https://www.jb51.net/database/3230048ux.htm
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作! |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
x
|