|
原始插入数据如下:
3 r% X. {. @6 q. v; f# @要求查询结果如下 :1 ^* w+ S% K" ?3 x; `$ s
/ v( x& _# A, X4 [$ L+ \' u' Y/ t1 m4 _. @
创建数据库、表
( d2 B/ i. f1 b+ H& T1 e1 b3 F- create database tests;
' u# g1 @" k1 y. }* @* ^ - use tests;( s S+ J" [! |" e! I
- create table t_score(
6 o4 P, R& J+ t: y& \2 M2 O# g - id int primary key auto_increment,2 E( t# k5 ?( ~
- name varchar(20) not null, #名字$ t: z" a9 ]4 X+ n( X; l/ m! E
- Subject varchar(10) not null, #科目! L# f3 m# g( f" ?8 k* C
- Fraction double default 0 #分数! |( y v/ R4 h, q
- );
复制代码添加数据 - INSERT INTO `t_score`(name,Subject,Fraction) VALUES
0 b) E( z% P' x- b2 ?( \ - ('王海', '语文', 86),
) K" L2 Z5 |# R9 ~! A; U - ('王海', '数学', 83),
8 n! y- q" D" R2 K/ ~, l - ('王海', '英语', 93),2 M, _' X _2 G# O* Y4 d2 m( I
- ('陶俊', '语文', 88),# F1 G- J2 [" q3 ]
- ('陶俊', '数学', 84),$ v+ T' V* ?& N
- ('陶俊', '英语', 94),
: b& n5 w* q5 P% j' {: l! p - ('刘可', '语文', 80),; e, x% k; D. S& X
- ('刘可', '数学', 86),& m" y$ P* c: r- F; |( C, O
- ('刘可', '英语', 88),) G( L. Y9 ]. `
- ('李春', '语文', 89),- j- @( e" v j, y
- ('李春', '数学', 80),
$ Q, {, y4 j& l6 p ?8 m! ^, D - ('李春', '英语', 87);
复制代码方式一:使用if - select name as 名字 ,
, D! ?5 z# `6 p% U8 B - sum(if(Subject='语文',Fraction,0)) as 语文,
9 }9 P+ `' M" Q/ k' W% @* ]! a - sum(if(Subject='数学',Fraction,0))as 数学, 5 s; x, h: h+ _, t# A' N
- sum(if(Subject='英语',Fraction,0))as 英语,
% C. t6 D) R7 i# R- q9 N/ n - round(AVG(Fraction),2) as 平均分,9 M/ A) O5 J r9 I
- SUM(Fraction) as 总分* z8 w- K* p9 J8 i
- from t_score group by name 6 v b, N/ p( v/ X) f. G7 ^+ J
- union) g; y2 g8 z ?9 P0 r
- select name as 名字 , sum(语文) Chinese,sum(数学) Math,sum(英语) English,round(AVG(总分),2)as 平均分,sum(总分) score from(
! r6 V9 I2 N3 R3 g5 S# Y - select 'TOTAL' as name,1 C" {6 m( Z8 u) ]" y
- sum(if(Subject='语文',Fraction,0)) as 语文,8 I r! W" U3 G) Q( I# }; f
- sum(if(Subject='数学',Fraction,0))as 数学,
9 Q# O( x0 R" Y( v/ | - sum(if(Subject='英语',Fraction,0))as 英语,
5 A' x7 _( }+ c - SUM(Fraction) as 总分6 Y- j* ~ J5 K1 D- h+ B* y9 ~$ e
- from t_score group by Subject )t
复制代码方式二:使用case - select name as Name,9 {* ~& @1 n2 L0 V3 P5 _& W
- sum(case when Subject = '语文' then Fraction end) as Chinese,, s! a6 ? X% _. z. d/ J
- sum(case when Subject = '数学' then Fraction end) as Math,0 v- V( [4 t. O" f
- sum(case when Subject = '英语' then Fraction end) as English,. s& z1 p9 v/ N6 @ A2 |
- sum(fraction)as score
9 V9 G: w: h' E5 o8 g$ ~ - from t_score group by name# b, q: T* L' x+ I! A4 a* r
- UNION ALL
8 }# z6 |3 y9 a9 F7 | - select name as Name,sum(Chinese) as Chinese,sum(Math) as Math,sum(English) as English,sum(score) as score from(
/ a \7 K' p% L0 Q7 u3 R' M - select 'TOTAL' as name,6 E+ t; U ?5 q: E7 H6 `
- sum(case when Subject = '语文' then Fraction end) as Chinese,
3 L" J) [9 `$ e: g, t3 j - sum(case when Subject = '数学' then Fraction end) as Math,& _9 Y4 ~ q% u! n
- sum(case when Subject = '英语' then Fraction end) as English,' t% f2 N' S% \5 I
- sum(fraction)as score2 ?* C3 r9 K& n2 }# ^6 E* K. G
- from t_score group by Subject)t
复制代码方法三: with rollup - select . f N6 o2 w. \, i4 O! m
- ifnull(name,'TOll') name,1 Z+ E! f: H; i$ R0 R
- sum(if(Subject='语文',Fraction,0)) as 语文,! D% W/ T) G7 A- D) W5 J1 j! K0 L2 x) _
- sum(if(Subject='英语',Fraction,0)) as 英语,4 t7 j7 T" t9 a% D7 Q, z- b3 Y+ f
- sum(if(Subject='数学',Fraction,0))as 数学,/ E! h0 t8 c( C0 c2 M7 |, g
- sum(Fraction) 总分
& M# G% |( ?. Q2 m6 g, O: T. P5 z% |5 G - from t_score group by name with rollup
复制代码查询结果如下:
0 X' ~9 b5 b# _; ?
; c* L8 s) F! e8 e. g6 b6 ~ |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
|