now() --当前程序执行的时间
sysdate() --当前函数执行的时间
datediff(time1,time2) --计算两个时间差
# 例子:获取20天以内的数据
select * from table_name
where datediff(now(),create_time) < 20;
dayofweek() --返回周几,从1开始数
-- 1 代表周日,2 代表周一,7代表周六
weekday() --返回周几,从0开始数
-- 0 代表周一
dayname(now()) -- 返回英文形式下周几
day() --每月几号
select day(now());
week() --第几周
select week(now());
month() -- 几月
select month(now));
year() -- xx年
select year(now());
# 例1.查找每月1号的订单
select * from order_1
where day(create_time) = 1;
# 例2.2019年1月哪一天的订单数最高
select * from order_1
where year(create_time) = 2019 and month(create_time) = 1
group by day(create_time)
order by count(*) desc
limit 1;
--------------------------------------------
-- 时间戳 和 日期格式 相互转换
# 将日期转为时间戳
unix_timestamp(now());
# 将时间戳转为日期
from_unixtime(时间戳);
SQL时间操作常用语句
最新推荐文章于 2023-06-03 20:10:29 发布