select '2015-08-11' as date,year('2015-08-11') as year
确定一个日期是一年中的第几个季度,可以用QUARTER()函数实现,代码如下:
SELECT '2015-08-11' AS DATE,QUARTER('2015-08-11') AS QUARTER
返回一个日期是一年中的月份,利用month()函数实现,代码如下:
SELECT '2015-08-11' AS DATE,MONTH('2015-08-11') AS MONTH
获取一个确定日期是一个月份中的多少号,可以用day()函数求得,具体实现代码如下:
SELECT '2015-08-11' AS DATE,DAY('2015-08-11') AS DAY
有时日期中带有时间,而如何获取小时数,这可以利用自带的函数hour()实现,代码如下:
SELECT '2015-08-11 12:20:45' AS DATE,HOUR('2015-08-11 12:20:45') AS HOUR
求取日期时间中的分钟数,可以利用MINUTE()函数实现,代码如下:
SELECT '2015-08-11 12:20:45' AS DATE,MINUTE('2015-08-11 12:20:45') AS MINUTE
最后,获取时间中的秒数,利用SECOND()函数实现,代码如下:
SELECT '2015-08-11 12:20:45' AS DATE,SECOND('2015-08-11 12:20:45') AS SECOND
下面以比较流行的mysql图形化管理工具Navicat为例,其他工具或者在命令行中以及编程语言中 *** 作时的执行的sql语句是一样的。
1、假设在数据库中有一个名为testtest的表格,表格内容如下图所示,表中有三条记录是9月份的
2、打开一个查询窗口,输入查询语句select * from testtest where month(date)='9',该语句表示查询testtest表格中9月份的记录
3、点击“运行”执行该sql语句,在下方可以看到已经查询到了9月份的三条记录
4、如需按年查询可输入select * from testtest where year(date)='2017',2017代表需要查询的年份。如下图所示只查询到了一条记录是2017年的
WHEREYear( 订购日期 ) = 2004 AND MONTH ( 订购日期 ) = 5
就可以了。
注: MySQL 中, 获取 日期的组成部分, 有2套函数可以使用, 下面是执行的例子代码:
mysql>SELECT 'NOW' DisplayMode, NOW() B
->UNION ALL
->SELECT 'Second' DisplayMode, Second(NOW()) B
->UNION ALL
->SELECT 'Minute' DisplayMode, MINUTE(NOW()) B
->UNION ALL
->SELECT 'Hour' DisplayMode, Hour(NOW()) B
->UNION ALL
->SELECT 'Day' DisplayMode, DAY(NOW()) B
->UNION ALL
->SELECT 'Week' DisplayMode, Week(NOW()) B
->UNION ALL
->SELECT 'Month' DisplayMode, Month(NOW()) B
->UNION ALL
->SELECT 'Year' DisplayMode, Year(NOW()) B
+-------------+---------------------+
| DisplayMode | B |
+-------------+---------------------+
| NOW | 2013-04-07 09:29:54 |
| Second | 54 |
| Minute | 29 |
| Hour| 9 |
| Day | 7 |
| Week| 14 |
| Month | 4 |
| Year| 2013|
+-------------+---------------------+
8 rows in set (0.00 sec)
mysql>SELECT '年' AS name, EXTRACT(YEAR FROM NOW()) AS value
->UNION ALL
->SELECT '年月' AS name, EXTRACT(YEAR_MONTH FROM NOW()) AS value
->UNION ALL
->SELECT '月' AS name, EXTRACT(MONTH FROM NOW()) AS value
->UNION ALL
->SELECT '日' AS name, EXTRACT(DAY FROM NOW()) AS value
->UNION ALL
->SELECT '时' AS name, EXTRACT(HOUR FROM NOW()) AS value
->UNION ALL
->SELECT '分' AS name, EXTRACT(MINUTE FROM NOW()) AS value
->UNION ALL
->SELECT '秒' AS name, EXTRACT(SECOND FROM NOW()) AS value
+------+--------+
| name | value |
+------+--------+
| 年 | 2013 |
| 年月 | 201304 |
| 月 | 4 |
| 日 | 7 |
| 时 | 9 |
| 分 | 27 |
| 秒 | 43 |
+------+--------+
7 rows in set (0.00 sec)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)