如何在 mysql 的 SYSDATE() 中减去一个月? [英] How to substract one month in SYSDATE() in mysql?

查看:196
本文介绍了如何在 mysql 的 SYSDATE() 中减去一个月?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库日期就像2013-07-09"即8月9日.SYSDATE() 是2013-08-09",也就是 8 月 9 日.当我要使用这个 SYSDATE() 将数据导入数据库时​​,需要 08,即月份数是 9 月.所以我想在 SYSDATE() 中减去一个月.

My Database date is like "2013-07-09" that is 9th August. SYSDATE() is "2013-08-09" that is also 9th August. When i am going to take data into database using this SYSDATE() in takes 08 which is month number is September. So I want to subtract one month in SYSDATE().

推荐答案

您的数据库中发生了一些奇怪的事情.在我看来,系统时间设置不正确,或者其他问题.

Something strange is going on in your database. It looks to me like the system time is set incorrectly, or something else is wrong.

2013-07-09"是2013年7月的第9天.yyyy-mm-dd"是布局.2013-08-09"是2013年8月9日.

"2013-07-09" is the ninth of July, 2013. "yyyy-mm-dd" is the layout. "2013-08-09" is the ninth of August, 2013.

没有有效且正确设置的 MySQL 实例,其中2013-07-09"表示八月九日,反之亦然.08"月从来就不是九月.所以,如果你的日期有任何意义,你需要快速找到它的底部.DBMS 系统加载了大量数据和更新日期是一个持续的噩梦:我从艰苦的经验中知道这一点.

There is no valid and correctly set up MySQL instance where "2013-07-09" means the ninth of August or vice versa. Month "08" has never meant September. So, if your dates have any significance, you need to get to the bottom of this quickly. DBMS systems loaded with tons of data with fouled up dates are an ongoing nightmare: I know this from hard experience.

如果你需要whatever_date前一个月的日期,使用这个MySQL表达式.

If you need the date one month before whatever_date, use this MySQL expression.

DATE_SUB(whatever_date, INTERVAL 1 MONTH)

还有一点你需要知道:NOW()SYSDATE() 的意思相同,但有一点区别:NOW()> 在每个查询的执行过程中只计算一次,而 SYSDATE() 会在数据库引擎需要时重新计算.大多数程序员认为 NOW() 是更好的选择,因为它是确定性的.

There's something else you need to know: NOW() means the same thing as SYSDATE() with a small distinction: NOW() is evaluated just once during the execution of each query, whereas SYSDATE() is re-evaluated whenever the database engine wants to. Most programmers consider NOW() to be a better choice because it's deterministic.

这篇关于如何在 mysql 的 SYSDATE() 中减去一个月?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆