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;

相关推荐
安当加密1 天前
MySQL 数据库如何加密脱敏?TDE透明加密 + DBG数据库网关 双引擎加固实战
数据库·mysql·adb
IT技术分享社区1 天前
MySQL统计查询优化:内存临时表的正确打开方式
数据库·mysql·程序员
短剑重铸之日1 天前
7天读懂MySQL|Day 5:执行引擎与SQL优化
java·数据库·sql·mysql·架构
好记忆不如烂笔头abc1 天前
RECOVER STANDBY DATABASE FROM SERVICE xxx,ORA-19909
数据库
writeone1 天前
数据库习题
数据库
廋到被风吹走1 天前
【数据库】【Oracle】分析函数与窗口函数
数据库·oracle
陌北v11 天前
为什么我从 MySQL 迁移到 PostgreSQL
数据库·mysql·postgresql
北辰水墨1 天前
Protobuf:从入门到精通的学习笔记(含 3 个项目及避坑指南)
数据库·postgresql
JIngJaneIL1 天前
基于java+ vue医院管理系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot
予枫的编程笔记1 天前
Redis 核心数据结构深度解密:从基础命令到源码架构
java·数据结构·数据库·redis·缓存·架构