写SQL时可以考虑的手段:
- 行转列
- 先分为多个临时表,然后JOIN到一起
-
select uid,t1.name YuWen,t2.name ShuXue from (select uid,namefrom tableAwhere naem = '语文') t1join (select uid,namefrom tableAwhere naem = '数学') t2on t1.uid = t2.uid;
- 用sum(if())
-
select uid,sum(if(name = '语文')) YuWen,sum(if(name = '数学')) ShuXue from tableA group by uid;
- 列转行
- 先分为多个临时表,然后UNION到一起
-
select uid,YuWen from tableAunion allselect uid,ShuXue from tableA;