sql语句获取今天、昨天、近7天、本周、上周、本月、上月、半年数据

查询今天的信息记录:

select * from `article` where to_days(`add_time`) = to_days(now());

查询昨天的信息记录:

select * from `article` where to_days(now()) – to_days(`add_time`) <= 1;

查询近7天的信息记录:

select * from `article` where date_sub(curdate(), INTERVAL 7 DAY) <= date(`add_time`);

查询近30天的信息记录:

select * from `article` where date_sub(curdate(), INTERVAL 30 DAY) <= date(`add_time`);

查询本月的信息记录:

select * from `article` where date_format(`add_time`, ‘%Y%m') = date_format(curdate() , ‘%Y%m');

查询上一月的信息记录:

select * from `article` where period_diff(date_format(now() , ‘%Y%m') , date_format(`add_time`, ‘%Y%m')) =1; 

查询
今天

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

同时,再附上 一个 mysql官方的相关document

 1 #查询本季度数据
 2 select * from `ht_invoice_information` where QUARTER(create_date)=QUARTER(now());
 3 #查询上季度数据
 4 select * from `ht_invoice_information` where QUARTER(create_date)=QUARTER(DATE_SUB(now(),interval 1 QUARTER));
 5 #查询本年数据
 6 select * from `ht_invoice_information` where YEAR(create_date)=YEAR(NOW());
 7 #查询上年数据
 8 select * from `ht_invoice_information` where year(create_date)=year(date_sub(now(),interval 1 year));
 9  
10 查询当前这周的数据 
11 SELECT name,submittime FROM enterprise WHERE YEARWEEK(date_format(submittime,'%Y-%m-%d')) = YEARWEEK(now());
12 查询上周的数据
13 SELECT name,submittime FROM enterprise WHERE YEARWEEK(date_format(submittime,'%Y-%m-%d')) = YEARWEEK(now())-1;
14 查询当前月份的数据
15 select name,submittime from enterprise   where date_format(submittime,'%Y-%m')=date_format(now(),'%Y-%m')
16 查询距离当前现在6个月的数据
17 select name,submittime from enterprise where submittime between date_sub(now(),interval 6 month) and now();
18 查询上个月的数据
19 select name,submittime from enterprise   where date_format(submittime,'%Y-%m')=date_format(DATE_SUB(curdate(), INTERVAL 1 MONTH),'%Y-%m')
20 select * from ` user ` where DATE_FORMAT(pudate, ' %Y%m ' ) = DATE_FORMAT(CURDATE(), ' %Y%m ' ) ;
21 select * from user where WEEKOFYEAR(FROM_UNIXTIME(pudate,'%y-%m-%d')) = WEEKOFYEAR(now())
22 select * 
23 from user 
24 where MONTH (FROM_UNIXTIME(pudate, ' %y-%m-%d ' )) = MONTH (now())
25 select * 
26 from [ user ] 
27 where YEAR (FROM_UNIXTIME(pudate, ' %y-%m-%d ' )) = YEAR (now())
28 and MONTH (FROM_UNIXTIME(pudate, ' %y-%m-%d ' )) = MONTH (now())
29 select * 
30 from [ user ] 
31 where pudate between 上月最后一天
32 and 下月第一天
33 where   date(regdate)   =   curdate();
34 select   *   from   test   where   year(regdate)=year(now())   and   month(regdate)=month(now())   and   day(regdate)=day(now())
35 SELECT date( c_instime ) ,curdate( )
36 FROM `t_score`
37 WHERE 1
38 LIMIT 0 , 30

 

posted @ 2022-08-03 17:16  酷盖的小机灵  阅读(2741)  评论(0编辑  收藏  举报