Mysql
 sql >> Cơ Sở Dữ Liệu >  >> RDS >> Mysql

Nhận mySQL MONTH () để sử dụng các số 0 ở đầu?

Sử dụng những thứ sau để thay thế:

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

Giải thích:

DATE_FORMAT() hàm cho phép bạn định dạng ngày tháng mà bạn thích bằng cách sử dụng các từ chỉ định được mô tả trong bảng bên dưới (lấy nguyên văn từ tài liệu ). Vì vậy, một chuỗi định dạng '%Y-%m' có nghĩa là:"Một năm đầy đủ (4 chữ số), theo sau là dấu gạch ngang (- ), theo sau là số tháng có hai chữ số ".

Lưu ý rằng bạn có thể chỉ định ngôn ngữ được sử dụng cho tên ngày / tháng bằng cách đặt lc_time_names biến hệ thống. Cực kỳ hữu ích. Tham khảo tài liệu để biết thêm chi tiết.

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 


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Không thể tạo bảng trong mysql -Error 1064

  2. SQL SERVER - Thủ thuật - Chạy SSMS với tài khoản Windows khác nhau

  3. Cách khắc phục lỗi kết nối cơ sở dữ liệu MySQL JDBC 08001

  4. Lấy dữ liệu từ bảng nối trong Yii2

  5. Cách xử lý có điều kiện phép chia cho 0 với MySQL