MySQL显示整个月的所有日期

时间:2013-10-23 08:53:41

标签: mysql date

如何显示整个月的所有日期,其中月份是查询的参数(例如1到3之间的月份),然后输出此查询将显示从1月的第一个日期到3月的最后一个日期的日期。也许预览如下。

2013-01-01
2013-01-02
2013-01-03
..
..
2013-02-28
..
2013-03-31

对此有任何想法吗?感谢您的帮助

7 个答案:

答案 0 :(得分:3)

如果您的表格的条目数大于您需要的日期数,那么您可以使用:

SELECT adddate('2013-01-01', @rownum := @rownum + 1) dt FROM my_table
JOIN (SELECT @rownum := -1) r
LIMIT 31;

my_table 选择一个小表。如果 my_table 包含太多条目,请使用限制以使其更快。

这个的基础是使用rownum这样的计数器自动增量。使用日期函数进行过滤的更复杂查询:

SELECT * FROM 
    (SELECT adddate('2013-01-01', @rownum := @rownum + 1) dt FROM my_table
    JOIN (SELECT @rownum := -1) r LIMIT 1000) temp
WHERE MONTH(dt) = 1 AND YEAR(dt) = 2015;

答案 1 :(得分:2)

例如,在2019年12月:

{
  this.state.isLoading ?
    <Show your Spinner /> :
    <FlatList
      data={this.state.dataSource}
      renderItem={renderItem}
      refreshing={this.state.isLoading}
      onRefresh={this._doApiCall}
      maxToRenderPerBatch={10}
      updateCellsBatchingPeriod={1000}
      initialNumToRender={1}
      removeClippedSubviews={false}
      keyExtractor={this._keyExtractor}
    />
}

它产生以下结果:

select FROM_UNIXTIME(UNIX_TIMESTAMP(CONCAT('2019-12-',n)),'%Y-%m-%d') as Date from (
        select (((b4.0 << 1 | b3.0) << 1 | b2.0) << 1 | b1.0) << 1 | b0.0 as n
                from  (select 0 union all select 1) as b0,
                      (select 0 union all select 1) as b1,
                      (select 0 union all select 1) as b2,
                      (select 0 union all select 1) as b3,
                      (select 0 union all select 1) as b4 ) t
        where n > 0 and n <= day(last_day('2019-12-01'))

答案 2 :(得分:1)

如果您需要生成两个日期之间所有日期的列表,您可以在tally(数字)表的帮助下执行此操作。您可以轻松创建一个这样的

CREATE TABLE tally (n int not null primary key);

INSERT INTO tally
SELECT a.N + b.N * 10 + c.N * 100 + 1 n
  FROM 
 (SELECT 0 AS N 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) a
,(SELECT 0 AS N 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) b
,(SELECT 0 AS N 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) c
ORDER BY n;

现在要创建1月1日到3月最后一天之间所有日期的列表,你可以这样做

SELECT '2013-01-01' + INTERVAL t.n - 1 DAY day
  FROM tally t
 WHERE t.n <= DATEDIFF(LAST_DAY('2013-03-01'), '2013-01-01') + 1

这是 SQLFiddle 演示

答案 3 :(得分:1)

从 MySQL 8.0(或 MariaDB 10.2.2)开始可以使用递归公用表表达式:

WITH RECURSIVE `days` AS
(
   SELECT 1 AS `day` UNION ALL SELECT `day` + 1 FROM `days` WHERE `day` < DAY(LAST_DAY(NOW()))
)
SELECT * FROM `days`

答案 4 :(得分:0)

select * from your_table
where month(`date`) between 1 and 3

如果你需要特定年份,你应该添加

select * from your_table
where year(`date`) = 2013
and month(`date`) between 1 and 3

答案 5 :(得分:0)

SELECT Field1, Field2 FROM table WHERE dateField between '2013-01-01' and '2013-03-31'

答案 6 :(得分:0)

我已对Brent Frere's answer进行了调整,以自动列出当月的所有日期,而没有对日期进行硬编码:

select FROM_UNIXTIME(UNIX_TIMESTAMP(CONCAT(year(now()),'-',month(now()),'-',n)),'%Y-%m-%d') as Date from (
        select (((b4.0 << 1 | b3.0) << 1 | b2.0) << 1 | b1.0) << 1 | b0.0 as n
                from  (select 0 union all select 1) as b0,
                      (select 0 union all select 1) as b1,
                      (select 0 union all select 1) as b2,
                      (select 0 union all select 1) as b3,
                      (select 0 union all select 1) as b4 ) t
        where n > 0 and n <= day(last_day(now()))
order by Date

或者,如果要使用MySQL变量设置年份和月份,则:

set @year = 2020, @month = 11;
select FROM_UNIXTIME(UNIX_TIMESTAMP(CONCAT(@year,'-',@month,'-',n)),'%Y-%m-%d') as Date from (
        select (((b4.0 << 1 | b3.0) << 1 | b2.0) << 1 | b1.0) << 1 | b0.0 as n
                from  (select 0 union all select 1) as b0,
                      (select 0 union all select 1) as b1,
                      (select 0 union all select 1) as b2,
                      (select 0 union all select 1) as b3,
                      (select 0 union all select 1) as b4 ) t
        where n > 0 and n <= day(last_day(CONCAT(@year,'-',@month,'-',1)))
order by Date