MySQL行列转换

创建一个sc表并插入数据

方法一:

select distinct uid,

(select score from sc where s.uid=uid and course='语文')语文,

(select score from sc where s.uid=uid and course='数学')数学,

(select score from sc where s.uid=uid and course='英语')英语

from sc s;

方法二:

select * from sc where course='语文';

select * from sc where course='数学';

select * from sc where course='英语';

把这三条数据当做三个表
select y.uid,y.score 语文,s.score 数学,e.score 英语

from (SELECT * FROM `sc` where course='语文') y

left join (SELECT * FROM `sc` where course='数学') s

on y.uid=s.uid

left join (SELECT * FROM `sc` where course='英语') e

on y.uid=e.uid;

相关推荐
green__apple6 分钟前
Oracle普通分区表转自动分区表
数据库·oracle
MediaTea6 分钟前
Python OOP 设计思想 13:封装服务于演化
linux·服务器·前端·数据库·python
清风拂山岗 明月照大江9 分钟前
MySQL 基础篇
数据库·sql·mysql
古城小栈11 分钟前
后端接入大模型实现“自然语言查数据库”
数据库·ai编程
IvorySQL12 分钟前
拆解 PostgreSQL 连接机制:从进程模型到通信协议
数据库·postgresql
Dxy123931021612 分钟前
MySQL连表更新讲解:从基础到高级应用
数据库·mysql
阿拉伯柠檬18 分钟前
MySQL复合查询
linux·数据库·mysql·面试
YongCheng_Liang19 分钟前
数据库核心概念深度解析:从基础原理到 SQL 分类
运维·数据库·sql
鲨莎分不晴19 分钟前
HBase 基本使用详解
大数据·数据库·hbase
霖霖总总21 分钟前
[小技巧28]MySQL 窗口函数详解:原理、用法与最佳实践
数据库·sql·mysql