MySQL中的日期函数
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
MySQL中的⽇期函数在MySQL中有⼀些常见的函数,总结如下:
1、当前⽇期
select curdate();
select current_date();
2、当前时间
select curtime();
select current_time();
3、当前⽇期和时间
select now();
select current_timestamp();
4、获取⽇期的年份、⽉份、⽇期
select year(current_date());
select month(current_date());
select day(current_date());
5、⽇期对应星期⼏
select dayname(current_date());
6、limit函数:⽤于限制查询结果返回的数量(注意:limit函数不⽀持SQL Server)
select * from 表名
limit i,n;
参数:
i:为查询结果的索引值(默认从0开始)
n:为查询结果返回的数量
7、查询组内前n条记录的函数
(1)MySQL
select column_name(s)
from table_name
limit i,n;
(2)SQL Server
select top numbers|percent
column_name(s)
from table_name;
(3)Oracle
select column_name(s)
from table_name
where rownum<=number;
8、时间差
(1)datediff函数:返回的差值是天数,不能定位到⼩时、分钟和秒。
-- 相差2天
select datediff('2018-03-22 09:00:00', '2018-03-20 07:00:00');
(2)timestampdiff函数:可以精确到天、⼩时、分钟和秒。
对于⽐较的两个时间,⼩的在前⾯,⼤的在后⾯。
相差1天:select TIMESTAMPDIFF(DAY, '2018-03-20 23:59:00', '2018-03-22 00:00:00');
相差49⼩时:select TIMESTAMPDIFF(HOUR, '2018-03-20 09:00:00', '2018-03-22 10:00:00');
相差2049分钟:select TIMESTAMPDIFF(MINUTE, '2018-03-20 09:00:00', '2018-03-22 10:00:00');
相差176400秒:select TIMESTAMPDIFF(SECOND, '2018-03-20 09:00:00', '2018-03-22 10:00:00');。