14

mysqlの2つの日付の間の月のリストを取得する必要があります。

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.
4

4 に答える 4

19

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
于 2013-02-11T13:38:41.867 に答える
5

これは実用的な解決策であり、そのように見たい場合はそれほど「エレガント」ではありませんが、機能し、いくつかのパラメーターを使用するだけで、関数やストアドプロシージャにすることができます...

まず、いくつかのレコードを含むテーブルが必要です。このテーブルを、行番号テーブルと同じように使用します。(表示したい同じ月数に対して同じ数の行が必要になります。テーブルが大きい方が良いです)>>

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

これにより、インスタンスにほとんどのレコードがある上位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);

説明:

1.日付変数を設定します2.月を追加するための月の値を設定します3.各行に対して日付を選択します(同じ行で月を追加し、月変数をインクリメントします)4.範囲内の日付をフィルターします5.出力フォーマット日付。

これが最終出力です>>

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
于 2017-08-04T19:39:06.580 に答える
3

これを試してください:

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
于 2013-02-11T12:13:40.807 に答える
0

以下のこのSQLは、私のMYSQLに役立ちました。任意の月の日付のリストを取得する必要に応じて、curdate()値を更新できます。リンク:(https://www.programmersought.com/article/26156494713/

SELECT DATE_FORMAT(DATE_SUB(last_day(curdate()), INTERVAL xc-1 day), '%Y-%m-%d') as date
FROM ( 
            SELECT @xi:=@xi+1 as xc from 
            (SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6) xc1, 
            (SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6) xc2,  
            (SELECT @xi:=0) xc0 
) xcxc) x0 where x0.date >= (select date_add(curdate(),interval-day(curdate())+1 day))
于 2021-07-23T08:47:07.733 に答える