Home > Database > Mysql Tutorial > body text

Mysql_日期增减有关问题

WBOY
Release: 2016-06-07 16:27:08
Original
1541 people have browsed it

Mysql_日期增减问题 select curdate(); ? ? ? ? ? ? ? ? ? ? ? #获取当前日期 select last_day(curdate()); ? ? ? ? ? ? ? ? ? ?#获取当月最后一天。 select DATE_ADD(curdate(),interval -day(curdate())+1 day); ? #获取本月第一天 select date_add(curdate

Mysql_日期增减问题

select curdate(); ? ? ? ? ? ? ? ? ? ? ? #获取当前日期

select last_day(curdate()); ? ? ? ? ? ? ? ? ? ?#获取当月最后一天。

select DATE_ADD(curdate(),interval -day(curdate())+1 day); ? #获取本月第一天

select date_add(curdate()-day(curdate())+1,interval 1 month); # 获取下个月的第一天

select date_add(curdate()-day(curdate())+1,interval -1 month); # 获取上个月的第一天

?

但是:由于各个月份天数有 28/29、30、31不等,故使用?-day(:stocktime)+1来获取上月或下月1号可能会出现问题

如下:

DATE_ADD(DATE_ADD(:stocktime,interval -1 month),interval -day(:stocktime)+1 day)

CONCAT(DATE_FORMAT(DATE_ADD(:stocktime,interval -1 month),'%y-%m'),'-01')

应改为:

DATE_ADD(:stocktime,interval -day(:stocktime)+1 day)

CONCAT(DATE_FORMAT(:stocktime,'%y-%m'),'-01')

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template