adli007
V2EX  ›  数据库

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

  •  
  •   adli007 · Apr 26, 2018 · 1721 views
    This topic created in 2953 days ago, the information mentioned may be changed or developed.
    怎么把这样一个表
    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  
       Apr 26, 2018
    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
       Apr 27, 2018
    @Efraim 多谢了 又学到了 if()函数
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1379 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 23:57 · PVG 07:57 · LAX 16:57 · JFK 19:57
    ♥ Do have faith in what you're doing.