召隆企博汇论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2377|回复: 0

mysql行列转换

[复制链接]

24

主题

5

回帖

199

积分

公司现有员工

积分
199
发表于 2019-12-9 11:14:57 | 显示全部楼层 |阅读模式
原始插入数据如下:+ \/ 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
  1. create database tests;
    + A4 o3 n* H$ `7 Y
  2. use tests;
    5 b: l0 l) O  H- o5 o2 I
  3. create table t_score(9 s$ e6 d( X3 r0 K
  4. id int primary key auto_increment,
    5 t6 a" V* h1 I# A; v" [$ i
  5. name varchar(20) not null,  #名字
    + f0 W" Q$ N$ [% ]- H4 D% ]
  6. Subject varchar(10) not null, #科目" o" a* d( [5 H" I
  7. Fraction double default 0  #分数
      B) c& M1 d9 a8 s0 p; S
  8. );
复制代码
添加数据
  1. INSERT INTO `t_score`(name,Subject,Fraction) VALUES
    & X* O4 t7 E1 M& n& v
  2.          ('王海', '语文', 86),0 t- R/ c! B1 P5 |0 Q, E. t
  3.         ('王海', '数学', 83),
    , o7 w8 k2 I4 b$ S; ^. C
  4.         ('王海', '英语', 93),
    8 T( n0 M4 T/ K  t. @8 g
  5.         ('陶俊', '语文', 88),
    - ~! J+ u8 ~9 V, H8 O
  6.         ('陶俊', '数学', 84)," T5 k3 u" M9 J" G
  7.         ('陶俊', '英语', 94),1 _8 |/ q- z' b9 \5 ?( e* E
  8.         ('刘可', '语文', 80),
    2 I- H+ w7 `3 R
  9.         ('刘可', '数学', 86),
    : `0 x2 k% [( ]' Y5 Z
  10.         ('刘可', '英语', 88),
    & Q% \7 }! w: ~. L: k$ |1 l6 i
  11.         ('李春', '语文', 89),
    - J# K- S$ s* e0 Z" s8 E
  12.         ('李春', '数学', 80),
    9 ~& z: |, a# q7 d1 k
  13.         ('李春', '英语', 87);
复制代码
方式一:使用if
  1. select name as 名字 ,# t& v) |, D9 c) y* w
  2. sum(if(Subject='语文',Fraction,0)) as 语文,
    " [4 J: L5 d9 h& j
  3. sum(if(Subject='数学',Fraction,0))as 数学, ' P- X6 g4 q: ~. `
  4. sum(if(Subject='英语',Fraction,0))as 英语,( p- D2 X/ o$ ~" F- b; H! F/ i
  5. round(AVG(Fraction),2) as 平均分,
    . T8 x5 `& u( j3 F+ N2 m3 J
  6. SUM(Fraction) as 总分
    , Y! ?, ]6 c, k+ g3 ^  l6 ~
  7. from t_score group by name     * O# {9 @% C8 T2 h9 P/ e$ _
  8. union1 B3 C* X+ C7 n2 e& \
  9. select name as 名字 , sum(语文) Chinese,sum(数学) Math,sum(英语) English,round(AVG(总分),2)as 平均分,sum(总分) score  from(9 R( z6 |  V  `9 B
  10. select 'TOTAL' as name,! X) J/ D4 R2 D' D
  11. sum(if(Subject='语文',Fraction,0)) as 语文,4 C7 G( t: A0 t2 q6 Q3 D$ X
  12. sum(if(Subject='数学',Fraction,0))as 数学, 7 v2 {" S& j' _  I  d# T2 E1 P
  13. sum(if(Subject='英语',Fraction,0))as 英语,/ ~% E# v$ K9 }& U3 m
  14. SUM(Fraction) as 总分( x: z' L# a9 d* L2 F
  15. from t_score group by Subject )t
复制代码
方式二:使用case
  1. select  name as Name,7 @2 j# N  U6 M' R- f8 m, R
  2. sum(case when Subject = '语文' then Fraction end) as Chinese,( K8 N: l4 {+ M) g* L. l$ Z
  3. sum(case when Subject = '数学' then Fraction end) as Math,) Y! \0 ]  q  |! D  c2 l
  4. sum(case when Subject = '英语' then Fraction end) as English,. d/ ?0 C" h  c% e* P
  5. sum(fraction)as score* [) q$ T7 f/ o7 P
  6. from t_score group by name
    9 R1 E7 b/ j, o. |& Y) u7 O
  7. UNION ALL
      X" v) }: n7 B7 N+ [
  8. 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
  9. select 'TOTAL' as name,6 e$ O5 l2 ]2 Y( X$ B. f2 j
  10. sum(case when Subject = '语文' then Fraction end) as Chinese,
    - N$ I9 a& n, ^9 s; `7 p$ l
  11. sum(case when Subject = '数学' then Fraction end) as Math,3 F: Z% W) X0 _8 g! t* d# }
  12. sum(case when Subject = '英语' then Fraction end) as English,
      N# u3 I+ m) O0 C
  13. sum(fraction)as score
    ' N8 E; P2 c! R' A- N/ C) ~5 q
  14. from t_score group by Subject)t
复制代码
方法三: with rollup
  1. select 9 L/ m0 C: `6 ?1 G: e7 o. {
  2.         ifnull(name,'TOll') name,
    ; @3 X4 s! @, s, Y; Z- O( I4 _
  3.         sum(if(Subject='语文',Fraction,0)) as 语文,
    5 l5 }3 q) O" X) b
  4.        sum(if(Subject='英语',Fraction,0)) as 英语,
    ; N* |, @8 r' f+ ^( W4 _
  5.        sum(if(Subject='数学',Fraction,0))as 数学,6 S! u5 ~) F9 H! k
  6.        sum(Fraction) 总分
      Q7 c+ Q' M+ \0 K0 Z) v
  7.         from t_score group by name with rollup
复制代码
查询结果如下:

: M) i) g$ t5 `! s0 }% F: G$ {5 h# y: P7 J- B

本帖子中包含更多资源

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

x
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-11-29 16:20 , Processed in 0.038324 second(s), 26 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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