Mysql 常用的時間查詢(周,月,季,年)

2021-07-25 19:02:46 字數 3244 閱讀 1040

一 查詢當日資料

1 select * from v2_goods_base_price as v2 where

date(v2.created_at) = curdate();

2 select * from v2_goods_base_price as v2

where

year(v2.created_at)=year(now())

andmonth(v2.created_at)=month(now())

andday(v2.created_at)=day(now());

3 select* from v2_goods_base_price as v2 where to_days(v2.created_at) = to_days(now());

二 查詢n天內的資料

select * from v2_goods_base_price as v2 

where v2.created_at > '****-**-**'

and v2.created_at <= date_add('****-**-**', interval n day);

三 查詢當前周資料

1 select * from v2_goods_base_price as v2 

where

month(v2.created_at)=month(now()) and

year(v2.created_at)=year(now());

2 select * from v2_goods_base_price as v2

where date_format(v2.created_at,'%y-%m')=date_format(now(),'%y-%m');

3 select * from v2_goods_base_price as v2

where

month(v2.created_at)=month(now()) and

year(v2.created_at)=year(now());

4 select * from v2_goods_base_price as v2

where

month(v2.created_at) = month(curdate())

四 查詢上週的資料

1 select * from v2_goods_base_price as v2 

where weekofyear(v2.created_at)=weekofyear(date_sub(now(),interval

1 week));

2 select * from v2_goods_base_price as v2

where v2.created_at>=date_add(now(),interval -(8 + weekday(now())) day)

and v2.created_at<=date_add(now(),interval -(1 + weekday(now())) day);

五 查詢當前月資料

1 select  * from v2_goods_base_price as v2

where

month(v2.created_at)=month(now()) and

year(v2.created_at)=year(now());

2 select * from v2_goods_base_price as v2

where date_format(v2.created_at,'%y-%m')=date_format(now(),'%y-%m');

3 select * from v2_goods_base_price as v2

where

month(v2.created_at)=month(now()) and

year(v2.created_at)=year(now());

4 select * from v2_goods_base_price as v2

where

month(v2.created_at) = month(curdate());

六 查詢上月的資料

select  * from v2_goods_base_price as v2

where date_format(v2.created_at,'%y-%m')=date_format(date_sub(curdate(), interval

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

七 查詢近n個月資料

select * from v2_goods_base_price as v2

where v2.created_at between date_sub(now(),interval n month) and now();

八 查詢當前季度資料

select * from v2_goods_base_price as v2

where quarter( v2.created_at ) = quarter( curdate( ))

九 查詢上n個季度的資料

select * from v2_goods_base_price as v2

where quarter(created_at)=quarter(date_sub(now(),interval n quarter));

十 查詢當前年份資料

select * from v2_goods_base_price as v2

where

year( v2.created_at ) = year( curdate( ))

十一 查詢前n年的資料

select  * from v2_goods_base_price as v2

where

year(v2.created_at)=year(date_sub(now(),interval n year));

十二 查詢某月某年資料

select * from 表名 where

year(date) =『2017』;

select * from 表名 where

month(date) =『03』;

取得時間(周 月 季度)

datetime dt datetime.now 當前時間 datetime startweek dt.adddays 1 convert.toint32 dt.dayofweek.tostring d 本週周一 datetime endweek startweek.adddays 6 本週週日 d...

Mysql 查詢天 周,月,季度 年的資料

今天 select from 表名 where to days 時間欄位名 to days now 昨天 select from 表名 where to days now to days 時間欄位名 1近7天 select from 表名 where date sub curdate interva...

mysql常用日期 時間查詢

好記性不如爛筆頭 select curdate 獲取當前日期 select last day curdate 獲取本月最後一天。select date add curdate interval day curdate 1day 獲取本月第一天 select date add curdate day ...