DBMS/MySQL

[MySQL] 임의로 숫자 리스트 만들기 (예제, 년도 리스트 출력)

antoroong 2025. 2. 17. 21:48

년도및, 날짜나, 시간별로 숫자 리스트를 임의로 만들어야 할 수도 있다.

나는 예제로, 2018 부터 2021까지의 숫자 리스트를 만들려고 한다,

 

 

select 
		year_list   
from (
    select 
		    @year := @year - 1 as year_list
    from 
				information_schema.tables a
	    , information_schema.tables b 
	    , (select @year := 2022) c_year
)year_ilst_cut
    
where  year_ilst_cut.year_list between '2018' and '2021';

 

 

 

또 다른 예

select 
    year_list,
    date_add('2021-01-01', interval year_list - 1 year) as year_date_add,
    Date_format(date_add('2021-01-01', interval year_list - 1 year),'%Y-%m-%d')as year_Date_format
from(
      select 
			    @year := @year +1 as year_list
	    from 
					information_schema.tables a
		    , information_schema.tables b 
		    , (select @year := 0) c_year
)x