MySQL的YEARWEEK函式以及查詢本週資料

2021-12-30 10:37:21 字數 2068 閱讀 1981

mysql的yearweek函式以及查詢本週資料

mysql 的 yearweek 是獲取年份和週數的乙個函式,函式形式為 yearweek(date[,mode]) 

例如 2010-3-14 ,禮拜天 

www.2cto.com  

select yearweek('2010-3-14') 返回 11 

select yearweek('2010-3-14',1) 返回 10 

其中第二個引數是 mode ,具體指的意思如下:  www.2cto.com  

mode

first day of week

range

week 1 is the first week …

0sunday

0-53

with a sunday in this year

1monday

0-53

with more than 3 days this year

2sunday

1-53

with a sunday in this year

3monday

1-53

with more than 3 days this year

4sunday

0-53

with more than 3 days this year

5monday

0-53

with a monday in this year

6sunday

1-53

with more than 3 days this year

7monday

1-53

with a monday in this 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` 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()) and month(from_unixtime(pudate,'%y-%m-%d')) = month(now());

select * from [user] where pudate between 上月最後一天 and 下月第一天;  

mysql的yearweek 函式轉換與逆轉換。

在sql開發中,經常會有按周進行group by,在將date處理成周時,mysql提供了很多方便的函式,例如week yearweek 等。其中yearweek 是包含年份的week 函式,能更方便的進行聚合。這次就提供一種對該函式的逆轉換方法。select curdate yearweek cu...

mysql日期函 MySQL 日期函式

mysql 日期函式 1,mysql dayofweek 和 weekday 獲取星期 在 mysql 中,我們可以用 dayofweek 和 weekday 函式獲取指定日期的星期.區別在於 dayofweek 獲取的星期索引是以 1 開始,而 weekday 獲取的星期索引是以 0 開始.day...

mysql時間函式中文 Mysql的時間函式

1.本週內的第幾天,從週日開始 mysql select dayofweek 2015 05 25 dayofweek 2015 05 25 2 1 row in set 0.00 sec 2.本月內的第幾天 mysql select dayofmonth 2015 05 25 dayofmonth...