MySQL查询以计算上个月 [英] MySQL Query to calculate the Previous Month
本文介绍了MySQL查询以计算上个月的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想计算上个月的总订单金额.
I would like to calculate total order amount in the previous month.
我得到了从当前日期开始获取本月数据的查询.
I got the query for getting the data for the present month from the current date.
SELECT SUM(goods_total) AS Total_Amount FROM orders
WHERE order_placed_date >= date_sub(current_date, INTERVAL 1 MONTH);
现在,我如何仅获取前一个月的数据,不包括本月.
Now how can I get Previous Months Data only, excluding this month.
例如这个月(7月)我赚了15,000美元,上个月(6月)我赚了14,000美元.
For e.g. This month (July) I made $15,000 and last Month(June) i made $14,000.
通过运行上述查询,我得到了$ 15,000.
I get the $15,000 by running the above query.
但是我不知道如何计算上个月.
But i dont know how to calculate Previous Months.
推荐答案
在这里,用它来获取MySQL中上个月1日到上月最后一个日期之间的日期:
Here you go, use this to get the date between the 1st of last month and the last of last month in MySQL:
... order_placed_date BETWEEN DATE_FORMAT(NOW() - INTERVAL 1 MONTH, '%Y-%m-01 00:00:00')
AND DATE_FORMAT(LAST_DAY(NOW() - INTERVAL 1 MONTH), '%Y-%m-%d 23:59:59')
这篇关于MySQL查询以计算上个月的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文