gpt4 book ai didi

mysql - 如何在只有一列是查询基础的 MySQL 中进行数据透视

转载 作者:行者123 更新时间:2023-11-29 06:27:55 25 4
gpt4 key购买 nike

我看到了很多关于如何在 MySQL 中进行数据透视的方法,但我无法将其与我的情况联系起来。

我的情况是,我在 tbl_members 中有一个名为 date_of_mem 的列。我需要计算在那个特定的月份和年份注册了多少成员(member)。

例如:

我有一个名为 tbl_members 的表,其中的列名为 idfnamemnamelname date_of_mem 等等。

现在,我想要这样的结果:

Year | January | February | March | April | May | June | July ...
2013 | 100 | 250 | 50 | 60 | 300 | 280 | 125 ...
2014 | 101 | 240 | 20 | 40 | 200 | 180 | 145 ...

如何在 MySQL 中执行此操作。

提前致谢...

最佳答案

select      year(date_of_mem) as yr,
sum(case when month(date_of_mem) = 1 then 1 else 0 end) as january,
sum(case when month(date_of_mem) = 2 then 1 else 0 end) as february,
sum(case when month(date_of_mem) = 3 then 1 else 0 end) as march,
sum(case when month(date_of_mem) = 4 then 1 else 0 end) as april,
sum(case when month(date_of_mem) = 5 then 1 else 0 end) as may,
sum(case when month(date_of_mem) = 6 then 1 else 0 end) as june,
sum(case when month(date_of_mem) = 7 then 1 else 0 end) as july,
sum(case when month(date_of_mem) = 8 then 1 else 0 end) as august,
sum(case when month(date_of_mem) = 9 then 1 else 0 end) as september,
sum(case when month(date_of_mem) = 10 then 1 else 0 end) as october,
sum(case when month(date_of_mem) = 11 then 1 else 0 end) as november,
sum(case when month(date_of_mem) = 12 then 1 else 0 end) as december
from tbl_members
group by year(date_of_mem)

关于mysql - 如何在只有一列是查询基础的 MySQL 中进行数据透视,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29787013/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com