简体   繁体   English

在只有一列作为查询基础的MySQL中如何做数据透视

[英]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_memtbl_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的表,其表名为idfnamemnamelnamedate_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.

相关问题 我如何在MySQL中查询两个日期,其中datefrom在同一表的第二列中? - How do I query two dates in mysql where the datefrom is in one column and dateto is in the second column of the same table? 如何使用mysql连接查询在一列中的两个位置? - how can use two where in one column with mysql join query? MYSQL仅在该表的列值与另一表的列值匹配的情况下,才如何从该表中选择数据? - MYSQL How do I select data from one table only where column values from that table match the column values of another table? MySQL查询返回的行比预期的多,我如何仅从每个组返回一列的最大值? - MySQL query returns more rows than intended, how do i only return the highest value of one column from each group? 如何在没有透视的mysql查询中将行转换为列 - How to convert a row into a column in mysql query without pivot 是否有可能仅在一个mysql查询中执行此操作 - would it possible to do this in one mysql query only MySQL的:如何只选择列(接受)= 1的地方? - Mysql: How to SELECT only where column (accepted) is = 1? MySQL通过多个数据透视表查询WHERE - MySQL Query WHERE through multiple pivot tables 如何在MySQL中查询以下数据透视表? - How do I query the following pivot table in MySQL? 如何使用 mysql 查询仅选择列值包含值的行 - How to use mysql query to select only rows where column value contains value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM