development

선행 0을 사용하려면 mySQL MONTH ()를 얻습니까?

big-blog 2020. 10. 11. 10:50
반응형

선행 0을 사용하려면 mySQL MONTH ()를 얻습니까?


이 쿼리에서 8 대신 '08'을 반환하도록 mySQL의 MONTH () 함수에 어떻게 지정합니까?

나는 정렬이 날짜별로 작동하고 싶습니다. 현재 다음과 같은 날짜에 대한 결과를 얻고 있습니다.

2006-9
2007-1
2007-10
2007-11

현재 검색어 :

SELECT COUNT(*), CONCAT(YEAR(`datetime_added`), '-', MONTH(`datetime_added`)) as date FROM `person` WHERE (email = '' OR email IS NULL) 
GROUP BY date 
ORDER BY date ASC

대신 다음을 사용하십시오.

DATE_FORMAT(`datetime_added`,'%Y-%m')

설명:

DATE_FORMAT()함수를 사용하면 아래 표에 설명 된 지정자를 사용하여 원하는 방식으로 날짜 형식을 지정할 수 있습니다 ( 문서 에서 그대로 가져옴 ). 따라서 형식 문자열 '%Y-%m'은 "전체 연도 (4 자리), 대시 ( -), 두 자리 월 숫자"를 의미합니다.

lc_time_names시스템 변수 를 설정하여 일 / 월 이름에 사용되는 언어를 지정할 수 있습니다 . 매우 유용합니다. 자세한 내용은 설명서 를 참조하십시오.

Specifier   Description
%a  Abbreviated weekday name (Sun..Sat)
%b  Abbreviated month name (Jan..Dec)
%c  Month, numeric (0..12)
%D  Day of the month with English suffix (0th, 1st, 2nd, 3rd, …)
%d  Day of the month, numeric (00..31)
%e  Day of the month, numeric (0..31)
%f  Microseconds (000000..999999)
%H  Hour (00..23)
%h  Hour (01..12)
%I  Hour (01..12)
%i  Minutes, numeric (00..59)
%j  Day of year (001..366)
%k  Hour (0..23)
%l  Hour (1..12)
%M  Month name (January..December)
%m  Month, numeric (00..12)
%p  AM or PM
%r  Time, 12-hour (hh:mm:ss followed by AM or PM)
%S  Seconds (00..59)
%s  Seconds (00..59)
%T  Time, 24-hour (hh:mm:ss)
%U  Week (00..53), where Sunday is the first day of the week
%u  Week (00..53), where Monday is the first day of the week
%V  Week (01..53), where Sunday is the first day of the week; used with %X
%v  Week (01..53), where Monday is the first day of the week; used with %x
%W  Weekday name (Sunday..Saturday)
%w  Day of the week (0=Sunday..6=Saturday)
%X  Year for the week where Sunday is the first day of the week, numeric, four digits; used with %V
%x  Year for the week, where Monday is the first day of the week, numeric, four digits; used with %v
%Y  Year, numeric, four digits
%y  Year, numeric (two digits)
%%  A literal “%” character
%x  x, for any “x” not listed above 

패딩을 다음과 같이 사용할 수 있습니다.

SELECT
    COUNT(*), 
    CONCAT(YEAR(`datetime_added`), '-', LPAD(MONTH(`datetime_added`), 2, '0')) as date 
FROM `person` 
WHERE (email = '' OR email IS NULL) 
GROUP BY date 
ORDER BY date ASC

DATE_FORMAT(`datetime_added`,'%Y - %m')

MONTH() returns an integer, so of course there's no leading zero. You will need to convert it to a string, left-pad the '0' and take the last 2 characters.

참고URL : https://stackoverflow.com/questions/7043794/get-mysql-month-to-use-leading-zeros

반응형