mysql日期时间查询及其格式化

编程

-- 今天  

select fullName,addedTime from t_user where to_days(addedTime) <= to_days(now()); 

-- 昨天  

select fullName,addedTime from t_user where to_days(NOW()) - TO_DAYS(addedTime) <= 1;  

-- 近7天  

select fullName,addedTime from t_user where date_sub(CURDATE(),INTERVAL 7 DAY) <= DATE(addedTime);  

-- 近30天  

SELECT fullName,addedTime FROM t_user where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(addedTime);

-- 本月  

SELECT fullName,addedTime FROM t_user WHERE DATE_FORMAT( addedTime, "%Y%m" ) = DATE_FORMAT( CURDATE() , "%Y%m" );

-- 上一月  

SELECT fullName,addedTime FROM t_user WHERE PERIOD_DIFF( date_format( now( ) , "%Y%m" ) , date_format( addedTime, "%Y%m" ) ) =1; 

-- 查询本季度数据  

select fullName,addedTime FROM t_user where QUARTER(addedTime)=QUARTER(now()); 

-- 查询上季度数据  

select fullName,addedTime FROM t_user where QUARTER(addedTime)=QUARTER(DATE_SUB(now(),interval 1 QUARTER));  

-- 查询本年数据  

select fullName,addedTime FROM t_user where YEAR(addedTime)=YEAR(NOW());  

-- 查询上年数据  

select fullName,addedTime FROM t_user where year(addedTime)=year(date_sub(now(),interval 1 year));  

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

select fullName,addedTime FROM t_user where addedTime between date_sub(now(),interval 6 month) and now();  

 

-- 查询当前这周的数据  

SELECT fullName,addedTime FROM t_user WHERE YEARWEEK(date_format(addedTime,"%Y-%m-%d")) = YEARWEEK(now());  

-- 查询上周的数据  

SELECT fullName,addedTime FROM t_user WHERE YEARWEEK(date_format(addedTime,"%Y-%m-%d")) = YEARWEEK(now())-1;  

-- 查询上个月的数据   

select fullName,addedTime FROM t_user where date_format(addedTime,"%Y-%m")=date_format(DATE_SUB(curdate(), INTERVAL 1 MONTH),"%Y-%m"); 

-- 查询当前月份的数据

select fullName,addedTime FROM t_user where DATE_FORMAT(addedTime,"%Y%m") = DATE_FORMAT(CURDATE(),"%Y%m");

select fullName,addedTime FROM t_user where date_format(addedTime,"%Y-%m")=date_format(now(),"%Y-%m"); 

 

-- 查询指定时间段的数据

select fullName,addedTime FROM t_user where addedTime between  "2017-1-1 00:00:00"  and "2018-1-1 00:00:00";   

select fullName,addedTime FROM t_user where addedTime >="2017-1-1 00:00:00"and addedTime < "2018-1-1 00:00:00";

获取当前日期:select curdate() 

获取当前日期前一天:select  date_sub(curdate(),interval 1 day)

获取当前日期后一天:select date_sub(curdate(),interval -1 day)

日期时间格式化:DATE_FORMAT(date_sub(NOW(),interval 1 day),"%Y-%m-%d %H:%i:%S")

举几个date_sub()的例子:

date_sub("2016-08-01",interval 1 day) 表示 2016-07-31
date_sub("2016-08-01",interval 0 day) 表示 2016-08-01
date_sub("2016-08-01",interval -1 day) 表示 2016-08-02

date_sub(curdate(),interval 1 day) 表示 2016-07-31
date_sub(curdate(),interval -1 day) 2016-08-02
date_sub(curdate(),interval 1 month) 表示 2016-07-01
date_sub(curdate(),interval -1 month) 表示 2016-09-01
date_sub(curdate(),interval 1 year) 表示 2015-08-01
date_sub(curdate(),interval -1 year) 表示 2017-08-01
 

查询一天前的数据

SELECT * FROM `TD_FRAMEWORK_Event_Notify` WHERE create_time <=DATE_FORMAT(date_sub(NOW(),interval 1 day),"%Y-%m-%d %H:%i:%S");

参考链接:

https://blog.csdn.net/ls1645/article/details/79118464

以上是 mysql日期时间查询及其格式化 的全部内容, 来源链接: utcz.com/z/517556.html

回到顶部