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;

相关推荐
wang2455981991 天前
【MySQL基础篇】概述及SQL指令:DDL及DML
sql·mysql·oracle
2401_898075121 天前
Python在金融科技(FinTech)中的应用
jvm·数据库·python
IvorySQL1 天前
PostgreSQL 技术日报 (3月14日)|AI 落地 PostgreSQL 拒绝 PPT 空谈
数据库·postgresql·开源
JavaGuide1 天前
鹅厂面试:SELECT * 一定导致索引失效?常见索引失效场景有哪些?
java·数据库·后端·mysql·大厂面试
wmfglpz881 天前
NumPy入门:高性能科学计算的基础
jvm·数据库·python
泯仲1 天前
从零起步学习MySQL 第十二章:MySQL分页性能如何优化?
数据库·学习·mysql
IvorySQL1 天前
直播预告|PostgreSQL 18.3 x IvorySQL 5.3:开启 AI 数据库新纪元
数据库·postgresql·开源
TDengine (老段)1 天前
TDengine IDMP 组态面板 —— 创建组态
大数据·数据库·物联网·时序数据库·iot·tdengine·涛思数据
SelectDB1 天前
Apache Doris + SelectDB:定义 AI 时代,实时分析的三大范式
大数据·数据库·数据分析