在MySQL 中可以透過以下三種方式完成
一起提取YEAR 和MONTH 然後我們可使用EXTRACT 函數。我們需要提供 YEAR_MONTH 作為此函數的參數。要理解它,請考慮使用表格「Collegedetail」中的資料的以下函數 -
mysql> Select EXTRACT(YEAR_MONTH From estb) from collegedetail; +-------------------------------+ | EXTRACT(YEAR_MONTH From estb) | +-------------------------------+ | 201005 | | 199510 | | 199409 | | 200107 | | 201007 | +-------------------------------+ 5 rows in set (0.00 sec)
可以集中或單獨提取年份和月份。顧名思義,我們也可以格式化其輸出。要理解它,請考慮以下範例,該範例使用表格「Collegedetail」中的資料-
mysql> Select DATE_FORMAT(estb, '%Y %m') from collegedetail; +----------------------------+ | DATE_FORMAT(estb, '%Y %m') | +----------------------------+ | 2010 05 | | 1995 10 | | 1994 09 | | 2001 07 | | 2010 07 | +----------------------------+ 5 rows in set (0.00 sec) mysql> Select DATE_FORMAT(estb, '%Y') from Collegedetail; +-------------------------+ | DATE_FORMAT(estb, '%Y') | +-------------------------+ | 2010 | | 1995 | | 1994 | | 2001 | | 2010 | +-------------------------+ 5 rows in set (0.00 sec) mysql> Select DATE_FORMAT(estb, '%m') from Collegedetail; +-------------------------+ | DATE_FORMAT(estb, '%m') | +-------------------------+ | 05 | | 10 | | 09 | | 07 | | 07 | +-------------------------+ 5 rows in set (0.00 sec) mysql> Select DATE_FORMAT(estb, '%M') from Collegedetail; +-------------------------+ | DATE_FORMAT(estb, '%M') | +-------------------------+ | May | | October | | September | | July | | July | +-------------------------+ 5 rows in set (0.10 sec)
它將使用兩個不同的函數分別提取年份和月份。要理解它,請考慮以下範例,該範例使用表格“Collegedetail”中的資料 -
mysql> Select YEAR(estb) AS 'Year', MONTH(estb) As 'MONTH' From collegedetail; +------+-------+ | Year | MONTH | +------+-------+ | 2010 | 5 | | 1995 | 10 | | 1994 | 9 | | 2001 | 7 | | 2010 | 7 | +------+-------+ 5 rows in set (0.00 sec)
以上是我們如何從 MySQL 中的日期中提取年份和月份?的詳細內容。更多資訊請關注PHP中文網其他相關文章!