召隆企博汇论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 483|回复: 0

mysql行列转换

[复制链接]

24

主题

5

回帖

199

积分

公司现有员工

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

0 X' ~9 b5 b# _; ?
; c* L8 s) F! e8 e. g6 b6 ~

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|召隆企博汇 ( 粤ICP备14061395号 )

GMT+8, 2025-1-20 17:05 , Processed in 0.034208 second(s), 26 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表