|
|
原始插入数据如下:+ \/ P8 _: f5 S1 z
要求查询结果如下 :- l+ O: p5 T0 z' q- d
2 c3 x/ c2 {1 A3 a* l, d! a. y6 s2 i
& h" O+ u2 |/ ^# ~4 Y
创建数据库、表4 Q7 K: U1 S2 ~& s: W( M, t
- create database tests;
+ A4 o3 n* H$ `7 Y - use tests;
5 b: l0 l) O H- o5 o2 I - create table t_score(9 s$ e6 d( X3 r0 K
- id int primary key auto_increment,
5 t6 a" V* h1 I# A; v" [$ i - name varchar(20) not null, #名字
+ f0 W" Q$ N$ [% ]- H4 D% ] - Subject varchar(10) not null, #科目" o" a* d( [5 H" I
- Fraction double default 0 #分数
B) c& M1 d9 a8 s0 p; S - );
复制代码添加数据 - INSERT INTO `t_score`(name,Subject,Fraction) VALUES
& X* O4 t7 E1 M& n& v - ('王海', '语文', 86),0 t- R/ c! B1 P5 |0 Q, E. t
- ('王海', '数学', 83),
, o7 w8 k2 I4 b$ S; ^. C - ('王海', '英语', 93),
8 T( n0 M4 T/ K t. @8 g - ('陶俊', '语文', 88),
- ~! J+ u8 ~9 V, H8 O - ('陶俊', '数学', 84)," T5 k3 u" M9 J" G
- ('陶俊', '英语', 94),1 _8 |/ q- z' b9 \5 ?( e* E
- ('刘可', '语文', 80),
2 I- H+ w7 `3 R - ('刘可', '数学', 86),
: `0 x2 k% [( ]' Y5 Z - ('刘可', '英语', 88),
& Q% \7 }! w: ~. L: k$ |1 l6 i - ('李春', '语文', 89),
- J# K- S$ s* e0 Z" s8 E - ('李春', '数学', 80),
9 ~& z: |, a# q7 d1 k - ('李春', '英语', 87);
复制代码方式一:使用if - select name as 名字 ,# t& v) |, D9 c) y* w
- sum(if(Subject='语文',Fraction,0)) as 语文,
" [4 J: L5 d9 h& j - sum(if(Subject='数学',Fraction,0))as 数学, ' P- X6 g4 q: ~. `
- sum(if(Subject='英语',Fraction,0))as 英语,( p- D2 X/ o$ ~" F- b; H! F/ i
- round(AVG(Fraction),2) as 平均分,
. T8 x5 `& u( j3 F+ N2 m3 J - SUM(Fraction) as 总分
, Y! ?, ]6 c, k+ g3 ^ l6 ~ - from t_score group by name * O# {9 @% C8 T2 h9 P/ e$ _
- union1 B3 C* X+ C7 n2 e& \
- select name as 名字 , sum(语文) Chinese,sum(数学) Math,sum(英语) English,round(AVG(总分),2)as 平均分,sum(总分) score from(9 R( z6 | V `9 B
- select 'TOTAL' as name,! X) J/ D4 R2 D' D
- sum(if(Subject='语文',Fraction,0)) as 语文,4 C7 G( t: A0 t2 q6 Q3 D$ X
- sum(if(Subject='数学',Fraction,0))as 数学, 7 v2 {" S& j' _ I d# T2 E1 P
- sum(if(Subject='英语',Fraction,0))as 英语,/ ~% E# v$ K9 }& U3 m
- SUM(Fraction) as 总分( x: z' L# a9 d* L2 F
- from t_score group by Subject )t
复制代码方式二:使用case - select name as Name,7 @2 j# N U6 M' R- f8 m, R
- sum(case when Subject = '语文' then Fraction end) as Chinese,( K8 N: l4 {+ M) g* L. l$ Z
- sum(case when Subject = '数学' then Fraction end) as Math,) Y! \0 ] q |! D c2 l
- sum(case when Subject = '英语' then Fraction end) as English,. d/ ?0 C" h c% e* P
- sum(fraction)as score* [) q$ T7 f/ o7 P
- from t_score group by name
9 R1 E7 b/ j, o. |& Y) u7 O - UNION ALL
X" v) }: n7 B7 N+ [ - select name as Name,sum(Chinese) as Chinese,sum(Math) as Math,sum(English) as English,sum(score) as score from(
3 i1 J' n3 W, F* ]0 I6 q: F+ A - select 'TOTAL' as name,6 e$ O5 l2 ]2 Y( X$ B. f2 j
- sum(case when Subject = '语文' then Fraction end) as Chinese,
- N$ I9 a& n, ^9 s; `7 p$ l - sum(case when Subject = '数学' then Fraction end) as Math,3 F: Z% W) X0 _8 g! t* d# }
- sum(case when Subject = '英语' then Fraction end) as English,
N# u3 I+ m) O0 C - sum(fraction)as score
' N8 E; P2 c! R' A- N/ C) ~5 q - from t_score group by Subject)t
复制代码方法三: with rollup - select 9 L/ m0 C: `6 ?1 G: e7 o. {
- ifnull(name,'TOll') name,
; @3 X4 s! @, s, Y; Z- O( I4 _ - sum(if(Subject='语文',Fraction,0)) as 语文,
5 l5 }3 q) O" X) b - sum(if(Subject='英语',Fraction,0)) as 英语,
; N* |, @8 r' f+ ^( W4 _ - sum(if(Subject='数学',Fraction,0))as 数学,6 S! u5 ~) F9 H! k
- sum(Fraction) 总分
Q7 c+ Q' M+ \0 K0 Z) v - from t_score group by name with rollup
复制代码查询结果如下:
: M) i) g$ t5 `! s0 }% F: G$ {5 h# y: P7 J- B
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
|