복붙노트

[SQL] MySQL은 두 날짜 사이의 개월의 목록을 얻는 방법

SQL

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.

해결법

  1. ==============================

    1.SQL 휘티 d 혀라도

    SQL 휘티 d 혀라도

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

    2.이것은 당신이 그런 식으로 그것을보고 싶어한다면 그렇게 '우아한'아니다, 실용적인 솔루션이지만, 그것을 작동, 당신은 단지 매개 변수의 부부와 함께, 그것을 기능 및 / 또는 저장 프로 시저를 만들 수 있습니다 ...

    이것은 당신이 그런 식으로 그것을보고 싶어한다면 그렇게 '우아한'아니다, 실용적인 솔루션이지만, 그것을 작동, 당신은 단지 매개 변수의 부부와 함께, 그것을 기능 및 / 또는 저장 프로 시저를 만들 수 있습니다 ...

    첫째, 우리는 몇 가지 기록, 모든 테이블과 테이블이 필요합니다.  우리는 단지 행 번호 테이블로,이 테이블을 사용하는거야. >> (당신은 큰 테이블이 더 필요, 당신이 표시 싶어 개월의 같은 양의 많은 행으로해야합니다)

    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 테이블을 사용하고, 당신을 말할 것이다, 그리고 그것의에 allways 거기는 ...

    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);
    

    설명 :

    이것은 >> 최종 출력

    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
    
  3. ==============================

    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
    
  4. from https://stackoverflow.com/questions/14811568/how-to-get-a-list-of-months-between-two-dates-in-mysql by cc-by-sa and MIT license