Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> MySQL

Kiểu dữ liệu lưu tháng và năm trong MySQL

Nên lưu DATETIME đầy đủ, rồi trích xuất tháng/năm bằng MONTH(), YEAR().

Cú pháp:

select MONTH(yourDateTimeColumnName) as anyVariableName1, YEAR(yourDateTimeColumnName) as anyVariableName2 from yourTableName;

Tạo bảng DueDateTime datetime, chèn 4 ngày.

Trích xuất:

mysql> select Month(DueDateTime) as OnlyMonth,Year(DueDateTime) as OnlyYear from OnlyMonthandYear;

Kết quả:

+-----------+----------+
| OnlyMonth | OnlyYear |
+-----------+----------+
| 12        | 2016     |
| 10        | 2017     |
| 3         | 2018     |
| 12        | 2018     |
+-----------+----------+
4 rows in set (0.02 sec)