|
|
原始插入数据如下:
0 \8 O& A4 L! g5 d) Y" k6 [8 Y要求查询结果如下 : t8 t. \2 r: P! ^8 h8 z
! J' a4 H2 H- s+ A0 B
" W X" m) p+ {( J3 P
创建数据库、表
1 ~9 P8 j1 T* ]1 k5 `# l- create database tests;8 ^0 c* b0 q8 n4 R8 Y
- use tests;0 w5 |; s, j6 C X
- create table t_score(
0 J1 P, }( r; f; q4 |% u - id int primary key auto_increment, p: n* C; z0 ^2 a
- name varchar(20) not null, #名字8 r |! W* C, k( x ~$ R
- Subject varchar(10) not null, #科目
) y) G* h9 X! P- c$ F( B - Fraction double default 0 #分数
B9 `" d/ y/ Z3 E+ X - );
复制代码添加数据 - INSERT INTO `t_score`(name,Subject,Fraction) VALUES
1 J) o1 Q& p( x6 |' y" e3 g - ('王海', '语文', 86),! y5 ] J$ U) _( b/ I8 E
- ('王海', '数学', 83),
* H& ?4 J9 J4 V - ('王海', '英语', 93),
/ u3 q0 V5 p* f/ N - ('陶俊', '语文', 88),
% j. K* k3 \% j4 o1 d9 C - ('陶俊', '数学', 84),$ ?1 U. i4 @! }* O ^
- ('陶俊', '英语', 94),! t$ [+ y! B# }- B i
- ('刘可', '语文', 80),' b* a1 B! j. u* \
- ('刘可', '数学', 86),+ Z* C) T- U7 Z3 e# n& P7 V) F) A1 _
- ('刘可', '英语', 88),
r. b- Z% b( m9 b& a - ('李春', '语文', 89)," j6 \& D: t4 B
- ('李春', '数学', 80),
, n( i( |4 x- s0 E - ('李春', '英语', 87);
复制代码方式一:使用if - select name as 名字 ,& _1 X' O S+ z v! j/ E# ]
- sum(if(Subject='语文',Fraction,0)) as 语文,8 |% \0 U7 S. i% m4 L) ~) M
- sum(if(Subject='数学',Fraction,0))as 数学, ( q+ r- n, a, D9 e
- sum(if(Subject='英语',Fraction,0))as 英语,- @# h1 b" h' p" g: h/ |* N6 i1 U
- round(AVG(Fraction),2) as 平均分,
, M' O5 q7 L) I: e+ r( H% f( ~* f. x - SUM(Fraction) as 总分6 p% E! A. ^- a* s- h
- from t_score group by name
T a2 Q: R( v8 v1 P& u - union+ I3 R, Q, N) l9 M, s9 D' O. u
- select name as 名字 , sum(语文) Chinese,sum(数学) Math,sum(英语) English,round(AVG(总分),2)as 平均分,sum(总分) score from(
+ W% Q! J$ c% i, ]& L+ B; V - select 'TOTAL' as name,9 z% ?4 l! p# M
- sum(if(Subject='语文',Fraction,0)) as 语文,
" ~- J# o- [2 h; r7 L7 E' m3 K, t - sum(if(Subject='数学',Fraction,0))as 数学,
- E6 D( D& |3 d$ `8 ]" o* g) Q - sum(if(Subject='英语',Fraction,0))as 英语,
8 G2 x: {3 t0 \- [0 g, s8 ~) R3 } - SUM(Fraction) as 总分" L" D8 i8 s- Z8 t( L
- from t_score group by Subject )t
复制代码方式二:使用case - select name as Name,
h! @4 G% w% X9 v) u - sum(case when Subject = '语文' then Fraction end) as Chinese,: J1 _) U8 y d1 F; X' S+ U* L9 [: w
- sum(case when Subject = '数学' then Fraction end) as Math,
5 k8 Q: k1 C7 N7 E - sum(case when Subject = '英语' then Fraction end) as English,: g- o/ X' ~0 ]7 g
- sum(fraction)as score; T# ~' X: O$ C& Y
- from t_score group by name# |, E2 E- p6 h) R9 w( v5 i
- UNION ALL. z5 t* L: x8 {
- select name as Name,sum(Chinese) as Chinese,sum(Math) as Math,sum(English) as English,sum(score) as score from(
# G# K* F4 h. O F6 S1 {- C - select 'TOTAL' as name,
5 [+ y9 T6 x! w+ {, r$ o! a - sum(case when Subject = '语文' then Fraction end) as Chinese,1 t! _" b0 x* N: `
- sum(case when Subject = '数学' then Fraction end) as Math,8 _+ a( T% o$ S# s" t
- sum(case when Subject = '英语' then Fraction end) as English,! b6 C1 z& R$ a% U
- sum(fraction)as score, s h, T' ~9 d+ b0 [" {1 j1 c. i
- from t_score group by Subject)t
复制代码方法三: with rollup - select
) b5 M8 O# [+ J- m( V* r - ifnull(name,'TOll') name,% n' w. ^/ U! J. ` F% t: b
- sum(if(Subject='语文',Fraction,0)) as 语文,: g+ `/ H- S$ X
- sum(if(Subject='英语',Fraction,0)) as 英语," R* S& f4 z: f; |7 M
- sum(if(Subject='数学',Fraction,0))as 数学,
9 z6 B2 W; [- O+ n' C! _ - sum(Fraction) 总分& M& [) o# V6 N! C* m1 }5 H
- from t_score group by name with rollup
复制代码查询结果如下:
6 [! b& ^" r9 A
: I; l2 x; k2 Z3 c0 b# O |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
|