【MySQL】時間に関するお問い合わせ、例えば今月、本年、今四半期

10890 ワード

-- mysql     
--   
select * from ticket_order_detail where to_days(use_time) = to_days(now());
-- 7 
SELECT *FROM ticket_order_detail  where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date( use_time)
--  30 
SELECT *FROM ticket_order_detail  where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date( use_time)
--   
SELECT *FROM ticket_order_detail  WHERE DATE_FORMAT(  use_time, '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' )
--    
SELECT *FROM ticket_order_detail  WHERE PERIOD_DIFF( date_format( now( ) , '%Y%m' ) , date_format(  use_time, '%Y%m' ) ) =1
#       
select * from `ticket_order_detail` where QUARTER(use_time)=QUARTER(now());
#       
select * from `ticket_order_detail` where QUARTER(use_time)=QUARTER(DATE_SUB(now(),interval 1 QUARTER));
#      
select * from `ticket_order_detail` where YEAR(use_time)=YEAR(NOW());
#      
select * from `ticket_order_detail` where year(use_time)=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`whereDATE_FORMAT(pudate,‘%Y%m‘)=DATE_FORMAT(CURDATE(),‘%Y%m‘) ;
select * from user where WEEKOFYEAR(FROM_UNIXTIME(pudate,’%y-%m-%d’)) = WEEKOFYEAR(now())
select*
fromuser
whereMONTH(FROM_UNIXTIME(pudate,‘%y-%m-%d‘))=MONTH(now())
select*
from[user]
whereYEAR(FROM_UNIXTIME(pudate,‘%y-%m-%d‘))=YEAR(now())
andMONTH(FROM_UNIXTIME(pudate,‘%y-%m-%d‘))=MONTH(now())
select*
from[user]
-- wherepudatebetween      
-- 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

 
転載先:https://www.cnblogs.com/flydkPocketMagic/p/7089324.html