[英]How to do Pivot in MySQL where only one column is the base of the query
I saw a lot of ways on how to do pivot in MySQL but I can't relate it in my situation. 我看到了很多如何在MySQL中进行数据透视的方法,但是我无法将其与我的情况联系起来。
My situation is, I have a column called date_of_mem
in tbl_members
. 我的情况是,我有一个名为列
date_of_mem
在tbl_members
。 I need to count how many member did register in that particular month and year. 我需要计算在那个特定月份和年份中有多少会员注册。
For Example: 例如:
I have a table called tbl_members
with column named id
, fname
, mname
, lname
and date_of_mem
and so forth. 我有一个名为
tbl_members
的表,其表名为id
, fname
, mname
, lname
和date_of_mem
等。
Now, I want to have this result: 现在,我想得到以下结果:
Year | January | February | March | April | May | June | July ...
2013 | 100 | 250 | 50 | 60 | 300 | 280 | 125 ...
2014 | 101 | 240 | 20 | 40 | 200 | 180 | 145 ...
How to do this in MySQL. 如何在MySQL中做到这一点。
Thanks in advance... 提前致谢...
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.