MySQL date_add() 如何使用月份插入?
大家好,我有这个 SQL,
SELECT DATE_ADD( '2009-'+ MONTH( NOW() ) +'-01' , INTERVAL -1 MONTH );
我无法让它工作,我在这里做错了什么?
坦克寻求帮助。
Hallo all, i have this SQL
SELECT DATE_ADD( '2009-'+ MONTH( NOW() ) +'-01' , INTERVAL -1 MONTH );
i can't get it to work, what i make wrong here?
tanks for help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是行不通的日期串联。它将字符串转换为数字,因此得到 2009+11+-1 = 2019,但无法转换为日期。
您可以使用 last_day 函数来获取当月的最后一天,添加一天以获取下个月的第二天,然后减去两个月以获取该月的第一天,而不是连接字符串中的日期。上个月:
It's the concatenation of the date that doesn't work. It converts the strings to numbers, so you get 2009+11+-1 = 2019, which then fails to convert to a date.
Instead of concatenating a date from strings, you can use the last_day function to get the last day of the current month, add one day to get to the next day of the next month, then subtract two months to get to the first day of the previous month:
Plus是算术运算符,必须使用concat。
或更好
(这也适用于 2010 年)
Plus is an arithmetical operator, you have to use concat.
or better
(this will also work in 2010)