如何获取mysql中两个日期之间的月份列表

时间:2022-01-20 21:30:34

I hve to get the list of months between two dates in mysql.

我想在mysql中获取两个日期之间的月份列表。

For Example:My Input is

 From date 23-01-2013
 To Date   01-04-2014

Output Should be 

Jan   2013,
Feb   2013,
March 2013,
.
.
.
Jan   2014,
Feb   2014,
Mar   2014,
Apr   2014.

3 个解决方案

#1


15  

SQLFiddle demo

SQLFiddle演示

select 
DATE_FORMAT(m1, '%b %Y')

from
(
select 
('2013-01-23' - INTERVAL DAYOFMONTH('2013-01-23')-1 DAY) 
+INTERVAL m MONTH as m1
from
(
select @rownum:=@rownum+1 as m from
(select 1 union select 2 union select 3 union select 4) t1,
(select 1 union select 2 union select 3 union select 4) t2,
(select 1 union select 2 union select 3 union select 4) t3,
(select 1 union select 2 union select 3 union select 4) t4,
(select @rownum:=-1) t0
) d1
) d2 
where m1<='2014-04-01'
order by m1

#2


3  

Try this one:

试试这个:

select aDate from (
  select @maxDate - interval (a.a+(10*b.a)+(100*c.a)+(1000*d.a)) day aDate 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) a, /*10 day range*/
  (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) b, /*100 day range*/
  (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) c, /*1000 day range*/
  (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) d, /*10000 day range*/
  (select @minDate := '2001-01-01', @maxDate := '2002-02-02') e
) f
where aDate between @minDate and @maxDate

#3


3  

this is a practical solution, it's not so 'elegant' if you wanna see it that way, but it works, and you can make it a function and/or a stored procedure, just with a couple of parameters...

这是一个实用的解决方案,它不是那么'优雅',如果你想看到它,但它的工作原理,你可以使它成为一个函数和/或存储过程,只需要几个参数...

first, we need a table with some records, any table. we're gonna use this table, just as a row number table. (you'll need as many rows for the same amount of months you wanna display, having a large table is better) >>

首先,我们需要一个包含一些记录的表,任何表。我们将使用此表,就像行号表一样。 (你想要显示相同数量的月份,你需要尽可能多的行,有一个大表更好)>>

SELECT CONCAT(table_schema, '.', table_name) as schema_table, table_rows
FROM   information_schema.TABLES
order by 2 desc limit 0,100

this will tell you, the top 100 tables with most records on your instance, i'm using the mysql.help table for this example, by default, this comes with some thousand records, and it's allways there...

这将告诉你,你的实例上有大多数记录的前100个表,我在这个例子中使用了mysql.help表,默认情况下,这里有几千条记录,而且它总是在那里......

set @start_date = '2013-01-23';
set @end_date = '2014-04-01';
set @months = -1;

select DATE_FORMAT(date_range,'%M, %Y') AS result_date from (
    select (date_add(@start_date, INTERVAL (@months := @months +1 ) month)) as date_range
    from mysql.help_topic a limit 0,1000) a
where a.date_range between @start_date and last_day(@end_date);

explained:

解释:

1. set date variales 2. set month value, for adding months 3. select for each row, a date (we add a month and increment the month variable on the same row) 4. filter dates that are between range 5. output formated dates.

this is the final output>>

这是最终的输出>>

January, 2013
February, 2013
March, 2013
April, 2013
May, 2013
June, 2013
July, 2013
August, 2013
September, 2013
October, 2013
November, 2013
December, 2013
January, 2014
February, 2014
March, 2014
April, 2014

#1


15  

SQLFiddle demo

SQLFiddle演示

select 
DATE_FORMAT(m1, '%b %Y')

from
(
select 
('2013-01-23' - INTERVAL DAYOFMONTH('2013-01-23')-1 DAY) 
+INTERVAL m MONTH as m1
from
(
select @rownum:=@rownum+1 as m from
(select 1 union select 2 union select 3 union select 4) t1,
(select 1 union select 2 union select 3 union select 4) t2,
(select 1 union select 2 union select 3 union select 4) t3,
(select 1 union select 2 union select 3 union select 4) t4,
(select @rownum:=-1) t0
) d1
) d2 
where m1<='2014-04-01'
order by m1

#2


3  

Try this one:

试试这个:

select aDate from (
  select @maxDate - interval (a.a+(10*b.a)+(100*c.a)+(1000*d.a)) day aDate 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) a, /*10 day range*/
  (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) b, /*100 day range*/
  (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) c, /*1000 day range*/
  (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) d, /*10000 day range*/
  (select @minDate := '2001-01-01', @maxDate := '2002-02-02') e
) f
where aDate between @minDate and @maxDate

#3


3  

this is a practical solution, it's not so 'elegant' if you wanna see it that way, but it works, and you can make it a function and/or a stored procedure, just with a couple of parameters...

这是一个实用的解决方案,它不是那么'优雅',如果你想看到它,但它的工作原理,你可以使它成为一个函数和/或存储过程,只需要几个参数...

first, we need a table with some records, any table. we're gonna use this table, just as a row number table. (you'll need as many rows for the same amount of months you wanna display, having a large table is better) >>

首先,我们需要一个包含一些记录的表,任何表。我们将使用此表,就像行号表一样。 (你想要显示相同数量的月份,你需要尽可能多的行,有一个大表更好)>>

SELECT CONCAT(table_schema, '.', table_name) as schema_table, table_rows
FROM   information_schema.TABLES
order by 2 desc limit 0,100

this will tell you, the top 100 tables with most records on your instance, i'm using the mysql.help table for this example, by default, this comes with some thousand records, and it's allways there...

这将告诉你,你的实例上有大多数记录的前100个表,我在这个例子中使用了mysql.help表,默认情况下,这里有几千条记录,而且它总是在那里......

set @start_date = '2013-01-23';
set @end_date = '2014-04-01';
set @months = -1;

select DATE_FORMAT(date_range,'%M, %Y') AS result_date from (
    select (date_add(@start_date, INTERVAL (@months := @months +1 ) month)) as date_range
    from mysql.help_topic a limit 0,1000) a
where a.date_range between @start_date and last_day(@end_date);

explained:

解释:

1. set date variales 2. set month value, for adding months 3. select for each row, a date (we add a month and increment the month variable on the same row) 4. filter dates that are between range 5. output formated dates.

this is the final output>>

这是最终的输出>>

January, 2013
February, 2013
March, 2013
April, 2013
May, 2013
June, 2013
July, 2013
August, 2013
September, 2013
October, 2013
November, 2013
December, 2013
January, 2014
February, 2014
March, 2014
April, 2014