简体   繁体   English

在一个查询中计算多个月

[英]Multiple months counts in one query

I have a mysql database in which i have a list of persons and the time data, so i'ts like我有一个 mysql 数据库,其中有人员列表和时间数据,所以我喜欢

user_firstname  | report_date
Alan            | 2020-04-12
Alex            | 2020-04-11
Miranda         | 2020-04-11
Joseph          | 2020-04-11
Alex            | 2020-04-10

I want to count for each user how many times did he report in a month我想为每个用户统计他在一个月内报告了多少次

SELECT user_firstname, user_lastname, count(*) as number 
FROM table 
WHERE MONTH(report_date) = 5 
GROUP BY user_email

and here I get the number of how many times did they do a report in 5th month, how can i connect all months in the same query?在这里我得到他们在第 5 个月做报告的次数,我怎样才能在同一个查询中连接所有月份?

my wish to get output like this:我希望像这样得到 output:

user_firstname  | Jan | Feb | Mar | Apr | May ....other months
Alan            |  2  |  6  | 10  |  1  |  8
Alex            |  5  |  1  |  3  |  7  |  28
Miranda         |  2  |  5  |  2  |  5  |  18
Joseph          |  1  |  0  |  0  | 25  |  8
Alex            |  7  |  9  |  1  |  2  |  58

You could use group by month() to您可以使用 group by month() 来

SELECT  MONTH(report_date), user_firstname, user_lastname, count(*) as number 
FROM table 
GROUP BY user_email, MONTH(report_date)

for what in your comment你的评论是什么

    SELECT   user_firstname, user_lastname
        , sum( month(report_date) = 1) Jan 
        , sum( month(report_date) = 2) Feb 
        , sum( month(report_date) = 3) Mar  
    FROM table 
    GROUP BY user_email

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM