/*上个月今天的当前时间*/
select date_sub(now(),interval 1 month) ;
/*上个月今天的当前时间(时间戳)*/
select UNIX_TIMESTAMP(date_sub(now(),interval 1 month)) ;
/*上个月的第一天*/
select date_sub(date_sub(date_format(now(),'%y-%m-%d 00:00:00'),interval extract( day from now())-1 day),interval 1 month);
/*上个月的第一天(时间戳)*/
select UNIX_TIMESTAMP(date_sub(date_sub(date_format(now(),'%y-%m-%d 00:00:00'),interval extract( day from now())-1 day),interval 1 month)) ;
/*上个月的第一天:*/
select date_sub(date_sub(date_format(now(),'%y-%m-%d'),interval extract(day from now())-1 day),interval 1 month);
/*上个月的最后一天:*/
select date_sub(date_sub(date_format(now(),'%y-%m-%d'),interval extract(day from now()) day),interval 0 month) as date;
/*这个月的第一天:*/
SELECT DATE_SUB(DATE_SUB(DATE_FORMAT(NOW(), '%y-%m-%d'), INTERVAL EXTRACT(DAY FROM NOW()) - 1 DAY), INTERVAL 0 MONTH);
/* 针对于 昨天 而言的 那个月 的 第一天*/
SELECT DATE_ADD(DATE_SUB(CURDATE(),INTERVAL 1 DAY), interval - day(DATE_SUB(CURDATE(),INTERVAL 1 DAY)) + 1 DAY);
/*这个月的最后一天:*/
select date_sub(date_sub(date_format(now(),'%y-%m-%d'),interval extract(day from now()) day),interval -1 month) as date;
/*获取本月最后一天:*/
select last_day(curdate());
/*获取本月第一天:*/
select date_add(curdate(), interval - day(curdate()) + 1 day);
/*获取下个月第一天:*/
select date_add(curdate() - day(curdate()) + 1, interval 1 month);
/*获取本月天数:*/
select day(last_day(curdate()));
/*获取一个月前那一天:*/
select date_sub(curdate(), interval 1 month);
/*今天 */
select * from 表名 where to_days(时间字段名) = to_days(now());
/*昨天 */
SELECT * FROM 表名 WHERE TO_DAYS( NOW( ) ) - TO_DAYS( 时间字段名) = 1 ;
/*昨天之前 昨天 */
SELECT * FROM 表名 WHERE TO_DAYS( NOW( ) ) - TO_DAYS( 时间字段名) > 1 ;
/*明天 */
SELECT * FROM 表名 WHERE TO_DAYS( 时间字段名) - TO_DAYS( NOW( ) ) = 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 DATE_SUB(curdate(),INTERVAL 0 DAY) ;
/*明天日期*/
select DATE_SUB(curdate(),INTERVAL -1 DAY) ;
/*昨天日期*/
select DATE_SUB(curdate(),INTERVAL 1 DAY) ;
/*前一个小时时间*/
select date_sub(now(), interval 1 hour);
/*后一个小时时间*/
select date_sub(now(), interval -1 hour);
/*前30分钟时间*/
select date_add(now(),interval -30 minute);
/*后30分钟时间*/
select date_add(now(),interval 30 minute);
/*今天*/
select * from 表名 where to_days(时间字段名) = to_days(now());
/*昨天*/
SELECT * FROM 表名 WHERE TO_DAYS( NOW( ) ) - TO_DAYS( 时间字段名) <= 1;
/*本周*/
SELECT * FROM 表名 WHERE YEARWEEK( date_format( 时间字段名,'%Y-%m-%d' ) ) = YEARWEEK( now() ) ;
/*本月*/
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 表名 WHERE YEAR( 时间字段名 ) = YEAR( NOW( ) ) ;
/*上一月*/
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');