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;

相关推荐
码农阿豪几秒前
离线部署指南:本地下载MySQL 8.0.37并上传服务器Centos7.9安装
服务器·mysql·adb
千寻技术帮26 分钟前
50013_基于微信小程序的校园志愿者系统
mysql·微信小程序·springboot·文档·ppt
艾斯比的日常33 分钟前
Redis 大 Key 深度解析:危害、检测与治理实践
数据库·redis·缓存
R.lin1 小时前
MySQL核心知识点梳理
数据库·mysql
百***06941 小时前
SQL JOIN:内连接、外连接和交叉连接(代码+案例)
数据库·sql·oracle
大数据魔法师1 小时前
MySQL(六) - 视图管理
数据库·mysql
Hello.Reader2 小时前
从 WAL 到 Fluss->Flink CDC Postgres Connector 端到端同步实战
数据库·flink
千桐科技2 小时前
数据库设计最佳实践:我们团队沉淀下来的规范
数据库·代码规范·设计
踏浪无痕2 小时前
PostgreSQL实例进程:从启动到运行的完整故事
数据库·postgresql
q***18842 小时前
redis的下载和安装详解
数据库·redis·缓存