Database/mysql

MySql 날짜 테이블 만들기

RightHot 2020. 9. 6. 12:59

1. 날짜테이블을 생성하기 위한 더미 테이블 생성


// 나중에 삭제할 테이블

create table t (n int);

insert into t values (1);

// 입력할 년도의 날짜 수만큼 행을 생성한다.  13번 반복시 대략 10년치 어차피 지울 테이블이라 적당히 처리
insert into t select * from t

2. 날짜테이블 생성

create table date_t (d date, ds char(8)); 
 
insert into date_t
select d, date_format(d, '%Y%m%d') from (
  select @rnum:=@rnum+1 as rownum, date(adddate('2014-01-01', interval @rnum day)) as d
  from (select @rnum:=-1) r, t
  ) t
where year(d) < 2024; 

[2차 출처] https://mydreamisthebestcooder.tistory.com/m/9