[MYSQL]특정 범위에 없는 날짜 데이터 만들기

2024. 5. 29. 13:43·Database
반응형

# YYYY-MM-DD

select * from 
(select adddate('1970-01-01',t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i) selected_date from
 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4) v
where selected_date between '2024-01-01' and '2024-12-31'

 

# YYYY-MM

select DISTINCT DATE_FORMAT(selected_date, '%Y-%m') AS selected_date from 
(select adddate('1970-01-01',t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i) selected_date from
 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4) v
where selected_date between '2024-01-01' and '2024-12-31'

 

 

출처 - https://www.infitry.com/8

반응형
저작자표시 비영리 변경금지 (새창열림)

'Database' 카테고리의 다른 글

[MYSQL]몇 년치 날짜데이터 테이블 만들기  (0) 2024.05.29
[MSSQL]매월 3째주 금요일 구하기(쿼리검증필요/주차관련)  (0) 2024.04.09
Elastic Search  (0) 2024.02.15
[mariadb] WITH 재귀쿼리 계층구조(조직도)  (0) 2024.02.15
[MYSQL]초단위 -> 일시분초  (0) 2023.04.14
'Database' 카테고리의 다른 글
  • [MYSQL]몇 년치 날짜데이터 테이블 만들기
  • [MSSQL]매월 3째주 금요일 구하기(쿼리검증필요/주차관련)
  • Elastic Search
  • [mariadb] WITH 재귀쿼리 계층구조(조직도)
애플자라
애플자라
    반응형
  • 애플자라
    애플자라
    애플자라
  • 전체
    오늘
    어제
    • 분류 전체보기 (655)
      • Linux (93)
      • Programing (92)
      • Flex, Laszlo (15)
      • Database (168)
      • Network (15)
      • Data (14)
      • Tips (119)
      • 여행★ (1)
      • Board (115)
      • 맛집탐방 (3)
      • 아이원츄 (12)
  • 블로그 메뉴

    • 홈
    • 태그
    • 미디어로그
    • 위치로그
    • 방명록
  • 링크

    • Love&Smile
    • Husk's repository
    • StartUp
    • DSDSTUDIO Experimentals
    • 신선
    • DEV.SEULKI.KR[이슬기]
    • Live Bit Block
  • 공지사항

  • 인기 글

  • 태그

    서버
    java
    delete
    오라클
    DATABASE
    MySQL
    Linux
    sql
    테이블
    table
    mssql
    파일
    eclipse
    리눅스
    Shell
    Windows
    Toad
    데이터
    user
    백업
    select
    설정
    server
    DB
    tomcat
    Oracle
    IP
    자동차
    윈도우
    apache
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
애플자라
[MYSQL]특정 범위에 없는 날짜 데이터 만들기
상단으로

티스토리툴바