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;

相关推荐
小陈工3 小时前
Python Web开发入门(十七):Vue.js与Python后端集成——让前后端真正“握手言和“
开发语言·前端·javascript·数据库·vue.js·人工智能·python
0xDevNull7 小时前
MySQL数据冷热分离详解
后端·mysql
科技小花7 小时前
数据治理平台架构演进观察:AI原生设计如何重构企业数据管理范式
数据库·重构·架构·数据治理·ai-native·ai原生
一江寒逸7 小时前
零基础从入门到精通MySQL(中篇):进阶篇——吃透多表查询、事务核心与高级特性,搞定复杂业务SQL
数据库·sql·mysql
D4c-lovetrain7 小时前
linux个人心得22 (mysql)
数据库·mysql
阿里小阿希8 小时前
CentOS7 PostgreSQL 9.2 升级到 15 完整教程
数据库·postgresql
荒川之神8 小时前
Oracle 数据仓库雪花模型设计(完整实战方案)
数据库·数据仓库·oracle
做个文艺程序员8 小时前
MySQL安全加固十大硬核操作
数据库·mysql·安全
不吃香菜学java8 小时前
Redis简单应用
数据库·spring boot·tomcat·maven
一个天蝎座 白勺 程序猿9 小时前
Apache IoTDB(15):IoTDB查询写回(INTO子句)深度解析——从语法到实战的ETL全链路指南
数据库·apache·etl·iotdb