[转]【MySQL】关于时间的查询,比如本月,本年,本季度

 原文地址:https://www.cnblogs.com/flydkPocketMagic/p/7089324.html

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

 

posted @ 2018-10-18 14:59  dirgo  阅读(530)  评论(0编辑  收藏  举报