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;

相关推荐
谅望者18 小时前
数据分析笔记07:Python编程语言介绍
大数据·数据库·笔记·python·数据挖掘·数据分析
面向星辰19 小时前
sql基本增删改查语句汇总
数据库·sql·mybatis
暂时先用这个名字19 小时前
信创时代下,PHP/MySQL应用的平滑迁移与运维管理升级(AI整理)
运维·mysql·php·信创·国产化·国产·迁移
一氧化二氢.h19 小时前
MySQL root用户连接错误解决方法
android·数据库·mysql
q***239220 小时前
数据库操作与数据管理——Rust 与 SQLite 的集成
数据库·rust·sqlite
q***333720 小时前
给SQL server数据库表字段添加注释SQL,附修改、删除注释SQL及演示
数据库·sql·oracle
百***221220 小时前
mysql 迁移达梦数据库出现的 sql 语法问题 以及迁移方案
数据库·sql·mysql
_Jimmy_20 小时前
ShardingSphere-JDBC 实现两个mysql数据库的不同表的关联查询
数据库·mysql
weixin_3077791320 小时前
基于AWS的应用程序可靠性提升架构优化方案——RDS多可用区与EC2弹性架构实践
数据库·数据仓库·架构·云计算·aws
Chan1620 小时前
【 Java八股文面试 | Redis篇 缓存问题、持久化、分布式锁 】
java·数据库·redis·后端·spring·缓存·面试