从MySQL中的表中获取两个日期之间的所有月份与计数

时间:2019-06-28 14:50:16

标签: mysql datetime difference

我已经写了一个查询,要求查询两天之间的全部月份,并以成员数为计数,但是无法获得全部月份。

SET @start_date = '2017-08-31'; 
SET @end_date = '2018-06-26'; 
SELECT COUNT(1) AS Y, DATE_FORMAT(created_date,'%b %y') AS X 
FROM members m 
WHERE 
    created_date >= @start_date AND 
    created_date <= @end_date 
GROUP BY X 
ORDER BY X;

跟随我得到的输出

1   Feb 18
1   Mar 18
1   Nov 17
2   Oct 17

但预期结果将是

2 Oct 17
1 Nov 17
0 Dec 17
0 Jan 18
1 Feb 18
1 Mar 18

在线查询LINK

2 个答案:

答案 0 :(得分:0)

请执行以下SQL以获得所需的输出: 选择EXTRACT(YEAR FROM created_date)AS YEAR,EXTRACT(MONTH FROM created_date)AS MONTH,COUNT(1) 来自成员 按年份,月份分组

注意到您也应用了排序,请按照您的需要执行相同的操作,它将获取带有年和月数的计数。

有关提取功能的更多详细信息,请参见以下链接: https://www.w3schools.com/sql/func_mysql_extract.asp

答案 1 :(得分:-1)

在要包括的所有月份中,您都需要一个“对照”表。这可以通过“日历”表来完成,也可以通过将“成员”表中所有月份的记录都包括为“占位符”来实现。

有一种更好的方法来完成此操作,但是,举例来说,这就是想法(使用上面LINK中的数据)

    CREATE TABLE members
(
    member_id INT,
    name VARCHAR(100),
    created_date DATE
);

INSERT INTO members (member_id, name, created_date) VALUES (1, 'Anand Jee', '2017/10/10');
INSERT INTO members (member_id, name, created_date) VALUES (2, 'Anil', '2017/11/21');
INSERT INTO members (member_id, name, created_date) VALUES (3, 'Saurav', '2018/2/10');
INSERT INTO members (member_id, name, created_date) VALUES (4, 'Amit', '2018/3/10');
INSERT INTO members (member_id, name, created_date) VALUES (5, 'Nilesh', '2018/7/17');
INSERT INTO members (member_id, name, created_date) VALUES (6, 'Nitesh', '2018/9/19');
INSERT INTO members (member_id, name, created_date) VALUES (7, 'Ajay', '2019/2/21');
INSERT INTO members (member_id, name, created_date) VALUES (8, 'Rahul', '2019/3/30');
INSERT INTO members (member_id, name, created_date) VALUES (9, 'Sumit', '2019/7/17');
INSERT INTO members (member_id, name, created_date) VALUES (10, 'Gautam', '2017/10/21');

INSERT INTO members (member_id, name, created_date) VALUES (-1, 'ZERO', '2017/08/1');
INSERT INTO members (member_id, name, created_date) VALUES (-1, 'ZERO', '2017/09/1');
INSERT INTO members (member_id, name, created_date) VALUES (-1, 'ZERO', '2017/10/1');
INSERT INTO members (member_id, name, created_date) VALUES (-1, 'ZERO', '2017/11/1');
INSERT INTO members (member_id, name, created_date) VALUES (-1, 'ZERO', '2017/12/1');

INSERT INTO members (member_id, name, created_date) VALUES (-1, 'ZERO', '2018/1/1');
INSERT INTO members (member_id, name, created_date) VALUES (-1, 'ZERO', '2018/2/1');
INSERT INTO members (member_id, name, created_date) VALUES (-1, 'ZERO', '2018/3/1');
INSERT INTO members (member_id, name, created_date) VALUES (-1, 'ZERO', '2018/4/1');
INSERT INTO members (member_id, name, created_date) VALUES (-1, 'ZERO', '2018/5/1');
INSERT INTO members (member_id, name, created_date) VALUES (-1, 'ZERO', '2018/6/1');
INSERT INTO members (member_id, name, created_date) VALUES (-1, 'ZERO', '2018/7/1');
INSERT INTO members (member_id, name, created_date) VALUES (-1, 'ZERO', '2018/8/1');
INSERT INTO members (member_id, name, created_date) VALUES (-1, 'ZERO', '2018/9/1');
INSERT INTO members (member_id, name, created_date) VALUES (-1, 'ZERO', '2018/10/1');
INSERT INTO members (member_id, name, created_date) VALUES (-1, 'ZERO', '2018/11/1');
INSERT INTO members (member_id, name, created_date) VALUES (-1, 'ZERO', '2018/12/1');

SET @start_date = '2017-08-31';
SET @end_date = '2018-06-26';
SELECT COUNT(1)-1 AS Y, DATE_FORMAT(created_date,'%b %y') AS X,DATE_FORMAT(created_date,'%y%m') AS Z
FROM members m
WHERE
created_date >= @start_date AND
created_date <= @end_date
GROUP BY X,Z
ORDER BY Z