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;

相关推荐
KIN_DIN18 分钟前
SQL 查询最新的一条记录
数据库·sql
m0_7066532342 分钟前
Python生成器(Generator)与Yield关键字:惰性求值之美
jvm·数据库·python
wangmengxxw43 分钟前
SpringAI-mysql
java·数据库·人工智能·mysql·springai
Coder_Boy_1 小时前
基于SpringAI的在线考试系统-数据库设计核心业务方案
java·数据库·spring boot·ddd·tdd
机器视觉知识推荐、就业指导1 小时前
Qt 元对象系统:机制、组成与典型用法
数据库·qt
qq_423233902 小时前
实战:用Python开发一个简单的区块链
jvm·数据库·python
信创天地2 小时前
国产化数据库深度运维:性能调优与故障排查实战指南
运维·数据库·安全·elk·自动化·rabbitmq
eWidget2 小时前
Shell输入输出(一):echo/printf输出,格式控制与颜色设置
运维·数据库·运维开发
a程序小傲3 小时前
得物Java面试被问:流批一体架构的实现和状态管理
java·开发语言·数据库·redis·缓存·面试·架构
Jess073 小时前
MySQL操作库 —— 库的操作
数据库·mysql