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
以上查询工作正常我从选定日期到选定日期获得30条记录。但是我希望显示完整的月份记录。如果我选择February
,则所有28天(如果闰年为29)记录将显示。与Mar
= 31
相同,记录April
= 30
条记录。等
编辑:查看截图。我想展示一个月内的所有日子。
如果有可能,请发表您的答案。
答案 0 :(得分:5)
如果您知道输入的年份和月份,那么您可以随时设置第一天,输入为Y = 2012 M=02
,第一天始终为2012-02-01
并使用该日期您可以获得最后一天一天,然后是该范围内的日期。
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)
答案 1 :(得分:1)
在寻找OP中列出的问题的答案时,恰好碰到了这篇文章,Abhik Chakraborty的答案非常出色。
如果您要从表中选择数据,并且希望列出一个月中的每一天,而不管表中是否有特定日期的数据,我想我都会发布一个示例,说明如何使用答案。月。
基本上,您要做的是使用上面的答案来创建派生表,您可以LEFT JOIN
现有表到此表,以便列出该月的每一天,并可以显示表中的数据存在的日子。该查询已准备好复制/粘贴,您所需要做的就是将对yourTable
的引用更改为要从中获取数据的表名。
这是示例查询:
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.
希望这会有所帮助,肯定会对我有所帮助。
答案 2 :(得分:0)
以下是两个查询的示例,可用于获取所需的所有记录。第一个查询将为您提供当前日期之前5个月的记录,第二个查询将为您提供当月第一天的记录。
这些只是示例,因此您可以调整这些查询以显示所需的相关信息。
时间戳/日期,可以是日期,也可以是时间戳,具体取决于您选择将日期存储到日期基础中的格式
当前日期前5个月的示例
select * from table where timestamp/date >= now() - interval 5 month;
月份第一天前5个月的例子
select * from table where timestamp/date >= last_day(now()) + interval 1 day - interval 5 month;