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;

相关推荐
行星0084 分钟前
Postgresql常用函数操作
数据库·postgresql
程序员葵安23 分钟前
【Java Web】9.Maven高级
java·数据库·后端·maven
Lx3521 小时前
UNION ALL与UNION的性能差异及选择技巧
sql·mysql·oracle
海棠一号1 小时前
Android Settings 数据库生成、监听与默认值配置
android·数据库
新时代苦力工1 小时前
MVCC机制:Undo Log版本链与ReadView机制
数据库·mysql
GUIQU.1 小时前
【Oracle】存储过程
数据库·oracle
爱可生开源社区2 小时前
SQLShift 重大更新:Oracle→PostgreSQL 存储过程转换功能上线!
数据库·postgresql·oracle
fengye2071612 小时前
板凳-------Mysql cookbook学习 (十)
学习·mysql·adb
蓝黑20202 小时前
使用SSH tunnel访问内网的MySQL
mysql·ssh·proxy
夕泠爱吃糖3 小时前
MySQL中的部分问题(1)
数据库·mysql