|
|
原始插入数据如下:
3 r# C+ u `# j" f _# p要求查询结果如下 :
& L! n3 m; k8 ^! [! S$ ^ [( k0 X$ R
3 L$ W% b5 L( M7 r3 c3 E, `( J: u
7 ?9 O8 Z: D! S; I创建数据库、表: O6 R9 G' [) }7 H$ C
- create database tests;6 H1 C% I/ ?; h' ?$ a
- use tests;9 A" l/ Z- o+ D- q. V
- create table t_score(# f. {& T% |, H1 Z1 B! m6 O
- id int primary key auto_increment,1 y; R5 n, b1 ?3 o; V6 X
- name varchar(20) not null, #名字
. G9 } p5 E7 h& \ - Subject varchar(10) not null, #科目 R0 h) p, ^% r& E& H
- Fraction double default 0 #分数
/ P# q8 j4 E1 [! \9 H* [$ i - );
复制代码添加数据 - INSERT INTO `t_score`(name,Subject,Fraction) VALUES/ X5 k' {" ^5 z/ j% y% K
- ('王海', '语文', 86),5 V5 S9 Z3 J$ J5 r* z2 _/ J
- ('王海', '数学', 83),
. E i' |# p, k3 A# Z2 @7 r& V - ('王海', '英语', 93),1 V4 C. `$ M) S1 Q2 `( G: o
- ('陶俊', '语文', 88),0 Q+ c8 y# ]- a% p" s2 w3 J
- ('陶俊', '数学', 84),# C8 f0 \; S- E8 f" M5 n2 Q5 p; f
- ('陶俊', '英语', 94),$ H: m D6 W. R( U
- ('刘可', '语文', 80),' K# z7 e G# T3 E. D$ l; l$ F" K! b
- ('刘可', '数学', 86),
4 E+ \( U0 M F9 L8 J" D- P y - ('刘可', '英语', 88),
4 l$ G" v3 t# G. M& k - ('李春', '语文', 89),! Y s$ G3 x7 ]! R9 D6 G4 o
- ('李春', '数学', 80),
. i1 {& U4 S/ Y - ('李春', '英语', 87);
复制代码方式一:使用if - select name as 名字 ,8 w: ^' l7 }- x" l5 P( A$ i' D+ j
- sum(if(Subject='语文',Fraction,0)) as 语文,
8 L3 C; [& v+ j - sum(if(Subject='数学',Fraction,0))as 数学,
6 E/ |$ \0 {9 Z - sum(if(Subject='英语',Fraction,0))as 英语,
* L3 a" U& b& f7 {; R - round(AVG(Fraction),2) as 平均分,
~/ w* |$ U2 T9 _( V4 O3 Y - SUM(Fraction) as 总分$ w1 k$ \6 E2 Z
- from t_score group by name
& w+ r4 [/ p" F# b2 ]9 [ - union: N% J" [& k6 X2 e; |; a
- select name as 名字 , sum(语文) Chinese,sum(数学) Math,sum(英语) English,round(AVG(总分),2)as 平均分,sum(总分) score from(, E1 F: f* f; L r5 P
- select 'TOTAL' as name,
$ a+ @1 X: d% B, I2 x+ |+ O - sum(if(Subject='语文',Fraction,0)) as 语文,# T/ W" w, K# T4 r) Z; j
- sum(if(Subject='数学',Fraction,0))as 数学, 9 C0 H& F j+ e" p( [
- sum(if(Subject='英语',Fraction,0))as 英语,
- @% b; y2 I, @% |- Q( ~/ o/ Q' s - SUM(Fraction) as 总分* D2 q5 O7 B4 W% N6 Z/ |
- from t_score group by Subject )t
复制代码方式二:使用case - select name as Name,8 a- g) o, l! N* r: d! |6 u
- sum(case when Subject = '语文' then Fraction end) as Chinese,
. `" j" a0 \; L; D* j - sum(case when Subject = '数学' then Fraction end) as Math,3 F q4 A0 `- H* S1 w
- sum(case when Subject = '英语' then Fraction end) as English,6 P' ^5 n' i, \% r0 A
- sum(fraction)as score, a7 z; d) ?2 D: e* x
- from t_score group by name6 E5 s" I5 t8 e/ O- y: m
- UNION ALL# F$ E$ O. s* ]( M1 S0 e
- select name as Name,sum(Chinese) as Chinese,sum(Math) as Math,sum(English) as English,sum(score) as score from(- T" e) G- f2 Z/ Q2 u
- select 'TOTAL' as name,) m+ g1 r. X) P& G# U
- sum(case when Subject = '语文' then Fraction end) as Chinese,
6 R, B* F* J4 n" S$ s* P6 h: A - sum(case when Subject = '数学' then Fraction end) as Math,
8 d& Y H; B# O4 @: H - sum(case when Subject = '英语' then Fraction end) as English,
5 a w5 L! N- F& U( @) M9 h - sum(fraction)as score6 _9 R- n. E$ S( c$ p' P0 ~: ?$ A0 a
- from t_score group by Subject)t
复制代码方法三: with rollup - select 3 A- s4 T7 s8 B1 `" }0 q. A
- ifnull(name,'TOll') name,
/ _/ G8 U9 P/ f2 Q+ O9 V' w - sum(if(Subject='语文',Fraction,0)) as 语文,, X2 u" d& i2 V& W& k# q. o
- sum(if(Subject='英语',Fraction,0)) as 英语," P2 v j+ G0 [# o) T
- sum(if(Subject='数学',Fraction,0))as 数学,
6 J, O6 O% s% `% B' c6 v9 I - sum(Fraction) 总分& z# r& Z9 g; n- c8 G
- from t_score group by name with rollup
复制代码查询结果如下: ( p3 z1 v- P9 w, L! p
' m& z4 g4 a- J0 h+ w
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
|