mysql查詢,本月 本週 本年

2021-08-21 20:55:12 字數 3602 閱讀 4649

mysql查詢,本月、本週、本年

--今天

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

1year));

--查詢當前這週的資料

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

6month) and now();

--查詢上個月的資料

select name,submittime from enterprise where date_format(submittime,'%y-%m')=date_format(date_sub(curdate(), interval

1month),'%y-%m')

select * from

` user `

where date_format(pudate, ' %y%m ' ) = date_format(curdate(), ' %y%m ' ) ;

select * from

user

where weekofyear(from_unixtime(pudate,'%y-%m-%d')) = weekofyear(now())

select *

from

user

where

month (from_unixtime(pudate, ' %y-%m-%d ' )) = month (now())

select *

from [ user ]

where

year (from_unixtime(pudate, ' %y-%m-%d ' )) = year (now())

andmonth (from_unixtime(pudate, ' %y-%m-%d ' )) = month (now())

select *

from [ user ]

where pudate between 上月最後一天

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

1limit 0 , 30

如果時間真接是儲存的時間戳形式在資料庫,就需要轉一下

#當天 select

sum(order_num),sum(profit_money) from t_wt_profit_waterstore

where branches_id=1

and to_days(from_unixtime(create_time/1000)) = to_days(now());

#本週 select

sum(order_num),sum(profit_money) from t_wt_profit_waterstore

where branches_id=11

and yearweek(from_unixtime(create_time/1000-3600*24)) = yearweek(now());

#本月 select

sum(order_num),sum(profit_money) from t_wt_profit_waterstore

where branches_id=11

and date_format(from_unixtime(create_time/1000),'%y%m') = date_format( curdate( ) ,'%y%m' )

本月少個括號

sql查詢本週本月本年

1.乙個月第一天的 select dateadd mm,datediff mm,0,getdate 0 2.本周一 select dateadd wk,datediff wk,0,getdate 0 select dateadd wk,datediff wk,0,getdate 6 3.一年的第一天...

MySQL日期查詢 本週 本月 本季 本年

查詢當天 select from 表名 where to days 時間欄位名 to days now 查詢昨天 select from 表名 where to days now to days 時間欄位名 1 查詢一周 select from 表名 where date sub curdate i...

MySQL日期查詢 本週 本月 本季 本年

查詢當天 select from 表名 where to days 時間欄位名 to days now 查詢昨天 select from 表名 where to days now to days 時間欄位名 1 查詢一周 select from 表名 where date sub curdate i...