MySQL行列互換的實(shí)現(xiàn)示例
場(chǎng)景1 行轉(zhuǎn)換列
1、表結(jié)構(gòu)和數(shù)據(jù)
/*
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 '學(xué)號(hào)',
`course_no` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '課程號(hào)',
`score_prize` decimal(4, 1) NULL DEFAULT NULL COMMENT '成績(jī)',
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、效果圖說(shuō)明,第一列用戶(hù)信息stu_no,第二列課程號(hào)course_no,第三列課程成績(jī)

| stu_no | 語(yǔ)文 | 數(shù)學(xué) | 英語(yǔ) |
|---|---|---|---|
| 0001 | 40.0 | 50.0 | 48.0 |
| 0002 | 40.0 | 30.0 | 99.0 |
| 0003 | 70.0 | 77.0 | 60.0 |
3、實(shí)現(xiàn)SQL
select stu_no,
sum(IF(course_no = '0001', score_prize, 0)) as '語(yǔ)文',
sum(IF(course_no = '0002', score_prize, 0)) as '數(shù)學(xué)',
sum(IF(course_no = '0003', score_prize, 0)) as '英語(yǔ)'
from score
group by stu_no;
場(chǎng)景2:列轉(zhuǎn)換行
1、準(zhǔn)備數(shù)據(jù)表結(jié)構(gòu)和數(shù)據(jù)
/*
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、效果圖

| name | course |
|---|---|
| 張三 | 90 |
| 張三 | 89 |
| 張三 | 79 |
| 李四 | 79 |
| 李四 | 88 |
| 李四 | 90 |
3、業(yè)務(wù)代碼
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;到此這篇關(guān)于MySQL行列互換的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)MySQL行列互換內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
mysql時(shí)間戳格式化函數(shù)from_unixtime使用的簡(jiǎn)單說(shuō)明
mysql中的FROM_UNIXTIME函數(shù)可以數(shù)據(jù)庫(kù)中整型類(lèi)的時(shí)間戳格式化為字符串的日期時(shí)間格式,下面這篇文章主要給大家介紹了關(guān)于mysql時(shí)間戳格式化函數(shù)from_unixtime使用的簡(jiǎn)單說(shuō)明,需要的朋友可以參考下2022-08-08
MySQL數(shù)據(jù)庫(kù)中Interval關(guān)鍵字的使用看這一篇就夠了
這篇文章主要給大家介紹了關(guān)于MySQL數(shù)據(jù)庫(kù)中Interval關(guān)鍵字使用的相關(guān)資料,interval作為一個(gè)關(guān)鍵字時(shí),表示為時(shí)間間隔,常用在date_add()、date_sub()、subdate(),函數(shù)中,常用于時(shí)間的加減法,需要的朋友可以參考下2024-08-08
MySQL group by和left join并用解決方式
這篇文章主要介紹了MySQL group by和left join并用解決方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
MySQL slave_net_timeout參數(shù)解決的一個(gè)集群?jiǎn)栴}案例
MySQL中的binlog相關(guān)命令和恢復(fù)技巧

