Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL - How to show all days records in particular month?

Tags:

php

mysql

select a.Date 
from (
    select curdate() - INTERVAL (a.a + (10 * b.a) + (100 * c.a)) DAY as Date
    from (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as a
    cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as b
    cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as c
) a 
where a.Date between '2015-04-15' and '2015-05-15' ORDER BY Date

Above query working fine I am getting 30 records from selected date to selected date. But I want to show complete month record. If i choose February then all 28 days(29 if leap year) record will showing. Same as Mar = 31 records April = 30 records. etc.

EDIT : See screenshot. I want to show all days in a month.

enter image description here

If possible to PHP please post your answer.

like image 386
Developer Avatar asked Dec 04 '25 16:12

Developer


2 Answers

If you know your input year and month then you can always set the first day, say the input is Y = 2012 M=02 ,the first day would be always 2012-02-01 and using that date you can get the last day and then the dates in that range. Something as

select a.Date 
from (
    select last_day('2012-02-01') - INTERVAL (a.a + (10 * b.a) + (100 * c.a)) DAY as Date
    from (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as a
    cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as b
    cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as c
) a 
where a.Date between '2012-02-01' and last_day('2012-02-01') order by a.Date;

+------------+
| Date       |
+------------+
| 2012-02-01 |
| 2012-02-02 |
| 2012-02-03 |
| 2012-02-04 |
| 2012-02-05 |
| 2012-02-06 |
| 2012-02-07 |
| 2012-02-08 |
| 2012-02-09 |
| 2012-02-10 |
| 2012-02-11 |
| 2012-02-12 |
| 2012-02-13 |
| 2012-02-14 |
| 2012-02-15 |
| 2012-02-16 |
| 2012-02-17 |
| 2012-02-18 |
| 2012-02-19 |
| 2012-02-20 |
| 2012-02-21 |
| 2012-02-22 |
| 2012-02-23 |
| 2012-02-24 |
| 2012-02-25 |
| 2012-02-26 |
| 2012-02-27 |
| 2012-02-28 |
| 2012-02-29 |
+------------+
29 rows in set (0.00 sec)
like image 162
Abhik Chakraborty Avatar answered Dec 06 '25 09:12

Abhik Chakraborty


Just came across this post while looking for an answer to the problem listed in the OP, Abhik Chakraborty's answer works fantastically.

I thought I would post an example of how to use the answer if you are looking to select data from a table and want to have every day in a month listed regardless if there is data in your table for a particular day in the month.

Basically what you have to do is to use the answer above to create a derived table that you can LEFT JOIN your existing table to so every day in the month will be listed and you can display data from your table for the days where it is present. This query is copy/paste ready, all you need to do is change the references to yourTable to be the table name you want to get the data from.

Here's the example query:

SELECT `dateList`.`Date`,
    CASE WHEN `yt`.`date` IS NULL THEN 0
    ELSE COUNT(`yt`.`id`) 
    END AS `amt`
FROM

-- this is the part you can copy/paste 
-- to be used to left join in a table of your choice 
-- ---------------------------------------------------------------------------------------------------------------------------
(
    SELECT `a`.`Date`
    FROM (
        SELECT LAST_DAY('2020-02-01') - INTERVAL (`a`.`a` + (10 * `b`.`a`) + (100 * `c`.`a`)) DAY AS `Date`
        FROM (SELECT 0 AS `a` UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS `a`
        CROSS JOIN (SELECT 0 AS `a` UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS `b`
        CROSS JOIN (SELECT 0 AS `a` UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS `c`
    ) AS `a` 
    WHERE `a`.`Date` between '2020-02-01' and LAST_DAY('2020-02-01')
) AS `dateList` 
-- ---------------------------------------------------------------------------------------------------------------------------

LEFT JOIN `yourTable` AS `yt` ON `dateList`.`Date` = DATE(`yt`.`date`)
GROUP BY `dateList`.`Date`
ORDER BY `dateList`.`Date` ASC

Here is the example in sql fiddle.

Hope this helps, it sure helped me.

like image 30
Gharbad The Weak Avatar answered Dec 06 '25 08:12

Gharbad The Weak