MySQL中日期問題

2021-06-05 17:15:14 字數 1003 閱讀 8292

這是一種方法:

select date_format(date_sub(current_date(),interval 1 month),'%y-%m-01 00:00:00') as prev_month_first,

date_format(last_day(date_sub(current_date(),interval 1 month)),'%y-%m-%d 23:59:59') as prev_month_end;

第二種方法:

上個月的第一天:

select date_sub(date_sub(date_format(now(),'%y-%m-%d'),interval extract(  

day from now())-1 day),interval 1 month)

上個月的最後一天:

select date_sub(date_sub(date_format(now(),'%y-%m-%d'),interval extract(  

day from now()) day),interval 0 month) as date

這個月的第一天:

select date_sub(date_sub(date_format(now(),'%y-%m-%d'),interval extract(  

day from now())-1 day),interval 0 month)

這個月的最後一天:

select date_sub(date_sub(date_format(now(),'%y-%m-%d'),interval extract(  

day from now()) day),interval -1 month) as date

至於你要查那乙個月啊,now())-1 day 和 interval 0 後面的數字自己去改就行了。

Mysql中日期處理

相信大家在專案中或多或少都會使用到日期比較這個功能,下面這些mysql函式可以滿足你的需求了 比較大小 比較兩個日期相隔的天數 將前面的日期減去後面的日期,忽略時分秒 結果是 6 select datediff 2017 7 8 15 24 08 2017 07 02 篩選出日期大於 2017 7 ...

MySQL中日期獲取

select last day curdate 獲取本月最後一天 select date add curdate interval day curdate 1 day 獲取本月第一天 select date add curdate day curdate 1,interval 1 month 獲取下...

mysql中日期轉換

w 星期名字 sunday saturday d 有英語字首的月份的日期 1st,2nd,3rd,等等。y 年,數字,4 位 y 年,數字,2 位 a 縮寫的星期名字 sun sat d 月份中的天數,數字 00 31 e 月份中的天數,數字 0 31 m 月,數字 01 12 c 月,數字 1 1...