写SQL时可以考虑的手段:
- 行转列
- 先分为多个临时表,然后JOIN到一起
-
select uid, t1.name YuWen, t2.name ShuXuefrom (select uid, name from tableA where naem = '语文') t1 join (select uid, name from tableA where naem = '数学') t2 on t1.uid = t2.uid;
- 用sum(if())
-
select uid, sum(if(name = '语文')) YuWen, sum(if(name = '数学')) ShuXuefrom tableAgroup by uid;
- 列转行
- 先分为多个临时表,然后UNION到一起
-
select uid, YuWenfrom tableAunion allselect uid, ShuXuefrom tableA;
来源地址:https://blog.csdn.net/qq_40382400/article/details/132062061