在 mysql 中可以通过以下三种方式完成
通过使用 extract() 函数 一起提取 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)
通过使用 date_format() 函数 可以集中或单独提取年份和月份。顾名思义,我们还可以格式化其输出。要理解它,请考虑以下示例,该示例使用表“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)
通过使用两个不同的函数 year() 和 month() 它将使用两个不同的函数分别提取年份和月份。要理解它,请考虑以下示例,该示例使用表“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 中的日期中提取年份和月份?的详细内容。
