目录

工欲善其事

实践出真知

活跃标签: linux java mysql 待分类 js springboot win10 电路 vue macOS nginx esp32 windows git docker idea maven esp8266 python Arduino

存档:

X

mysql 时间函数

基本时间

now(), current_timestamp(); -- 当前日期时间
current_date(); -- 当前日期
current_time(); -- 当前时间
date('yyyy-mm-dd hh:ii:ss'); -- 获取日期部分
time('yyyy-mm-dd hh:ii:ss'); -- 获取时间部分
date_format('yyyy-mm-dd hh:ii:ss', '%d %y %a %d %m %b %j'); -- 格式化时间
unix_timestamp(); -- 获得unix时间戳
from_unixtime(); -- 从时间戳获得时间

1. 当前日期

select DATE_SUB(curdate(),INTERVAL 0 DAY) ;

2. 明天日期

select DATE_SUB(curdate(),INTERVAL -1 DAY) ;

3. 昨天日期

select adddate(now(),-1)
select date_sub(now(),interval 1 day)

4. 前一个小时时间

select date_sub(now(), interval 1 hour);

5. 后一个小时时间

select date_sub(now(), interval -1 hour);

6. 前30分钟时间

select date_add(now(),interval -30 minute)

7. 后30分钟时间

select date_add(now(),interval 30 minute)

8. 根据format字符串格式化date值:

%S, %s  两位数字形式的秒( 00,01, ..., 59)
%I, %i  两位数字形式的分( 00,01, ..., 59)
%H  两位数字形式的小时,24 小时(00,01, ..., 23)
%h  两位数字形式的小时,12 小时(01,02, ..., 12)
%k  数字形式的小时,24 小时(0,1, ..., 23)
%l  数字形式的小时,12 小时(1, 2, ..., 12)
%T  24 小时的时间形式(hh:mm:ss)
%r  12 小时的时间形式(hh:mm:ss AM 或hh:mm:ss PM)
%p  AM或PM
%W  一周中每一天的名称(Sunday, Monday, ..., Saturday)
%a  一周中每一天名称的缩写(Sun, Mon, ..., Sat)
%d  两位数字表示月中的天数(00, 01,..., 31)
%e  数字形式表示月中的天数(1, 2, ..., 31)
%D  英文后缀表示月中的天数(1st, 2nd, 3rd,...)
%w  以数字形式表示周中的天数( 0 = Sunday, 1=Monday, ..., 6=Saturday)
%j  以三位数字表示年中的天数( 001, 002, ..., 366)
%U  周(0, 1, 52),其中Sunday 为周中的第一天
%u  周(0, 1, 52),其中Monday 为周中的第一天
%M  月名(January, February, ..., December)
%b  缩写的月名( January, February,...., December)
%m  两位数字表示的月份(01, 02, ..., 12)
%c  数字表示的月份(1, 2, ...., 12)
%Y  四位数字表示的年份
%y  两位数字表示的年份
%%  直接值“%”

当天

select * from 表名 where to_days(时间字段名) = to_days(now());

昨天

SELECT * FROM 表名 WHERE TO_DAYS( NOW( ) ) - TO_DAYS( 时间字段名) <= 1

7天

SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(时间字段名)

近30天

SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(时间字段名)

本月

SELECT * FROM 表名 WHERE DATE_FORMAT( 时间字段名, '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' )

上一月

SELECT * FROM 表名 WHERE PERIOD_DIFF( date_format( now( ) , '%Y%m' ) , date_format( 时间字段名, '%Y%m' ) ) =1

查询本季度数据

select * from `ht_invoice_information` where QUARTER(create_date)=QUARTER(now());

查询上季度数据

select * from `ht_invoice_information` where QUARTER(create_date)=QUARTER(DATE_SUB(now(),interval 1 QUARTER));

查询本年数据

select * from `ht_invoice_information` where YEAR(create_date)=YEAR(NOW());

查询上年数据

select * from `ht_invoice_information` where year(create_date)=year(date_sub(now(),interval 1 year));

查询当前这周的数据

SELECT name,submittime FROM enterprise WHERE YEARWEEK(date_format(submittime,'%Y-%m-%d')) = YEARWEEK(now());

查询上周的数据

SELECT name,submittime FROM enterprise WHERE YEARWEEK(date_format(submittime,'%Y-%m-%d')) = YEARWEEK(now())-1;

查询当前月份的数据

select name,submittime from enterprise   where date_format(submittime,'%Y-%m')=date_format(now(),'%Y-%m')

查询距离当前现在6个月的数据

select name,submittime from enterprise where submittime between date_sub(now(),interval 6 month) and now();

查询上个月的数据

select name,submittime from enterprise   where date_format(submittime,'%Y-%m')=date_format(DATE_SUB(curdate(), INTERVAL 1 MONTH),'%Y-%m')

select * from ` user ` where DATE_FORMAT(pudate, ' %Y%m ' ) = DATE_FORMAT(CURDATE(), ' %Y%m ' ) ;

select * from user where WEEKOFYEAR(FROM_UNIXTIME(pudate,'%y-%m-%d')) = WEEKOFYEAR(now())

select * 
from user 
where MONTH (FROM_UNIXTIME(pudate, ' %y-%m-%d ' )) = MONTH (now())

select * 
from [ user ] 
where YEAR (FROM_UNIXTIME(pudate, ' %y-%m-%d ' )) = YEAR (now())
and MONTH (FROM_UNIXTIME(pudate, ' %y-%m-%d ' )) = MONTH (now())

select * 
from [ user ] 
where pudate between 上月最后一天
and 下月第一天

where   date(regdate)   =   curdate();

select   *   from   test   where   year(regdate)=year(now())   and   month(regdate)=month(now())   and   day(regdate)=day(now())

SELECT date( c_instime ) ,curdate( )
FROM `t_score`
WHERE 1
LIMIT 0 , 30

标题:mysql 时间函数
作者:llilei
地址:http://solo.llilei.work/articles/2021/02/18/1613616395219.html