V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
adli007
V2EX  ›  数据库

[面试题]关于数据库查询 mysql

  •  
  •   adli007 · 2018-04-26 17:55:28 +08:00 · 822 次点击
    这是一个创建于 2162 天前的主题,其中的信息可能已经有所发展或是发生改变。
    怎么把这样一个表
    year  month amount
    1991   1      1.1
    1991    2      1.2
    1991    3      1.3
    1991    4      1.4
    1992    1      2.1
    1992    2      2.2
    1992    3      2.3
    1992    4      2.4
    查成这样一个结果
    year m1   m2   m3   m4
    1991 1.1 1.2 1.3 1.4
    1992 2.1 2.2 2.3 2.4
    Efraim
        1
    Efraim  
       2018-04-26 19:51:37 +08:00
    select year ,sum(if(month=1,amount,0)) as m1,sum(if(month=2,amount,0)) as m2,sum(if(month=3,amount,0)) as m3,sum(if(month=4,amount,0)) as m4 from test group by year;
    adli007
        2
    adli007  
    OP
       2018-04-27 00:56:05 +08:00
    @Efraim 多谢了 又学到了 if()函数
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5475 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 09:07 · PVG 17:07 · LAX 02:07 · JFK 05:07
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.