SQL SELECT和GROUP BY Month在9个月以上没有返回任何结果

I am receiving a weird result in this MySQL query.

I am attempting to return 'orders', grouped by month.

SELECT DATE_FORMAT(date_of_sale, '%b') AS date, SUM(quantity) AS items FROM order_elements JOIN orders ON order_elements.order_ref = orders.order_id WHERE cur_status = 1 AND quantity > 0 AND MONTH(date_of_sale) BETWEEN MONTH(CURDATE() - INTERVAL 8 MONTH) AND MONTH(NOW()) GROUP BY MONTH(date_of_sale) ORDER BY MONTH(date_of_sale) DESC

When I run the query to get orders between now and 8 months ago I get this result:

==================
| date   | items |
==================
| Sep    | 95    |
| Aug    | 110   |
| Jul    | 106   |
| Jun    | 110   |
| May    | 100   |
| Apr    | 130   |
| Mar    | 100   |
| Feb    | 94    |
| Jan    | 7     |
===================

January 30th is the EARLIEST AND FIRST date I have an order for. Ever other date_of_sale is later than Jan 30th.

When I run the same query but between now and 9 months ago, I get NO results.

I think it may have something to do with 9 months ago being equal to Dec and it falling onto a different year and causing some sort of issue, but I am not sure. Does anyone have any idea why searching back an extra month would return no results? Is it something to do with no data beyond Jan, or the Month being of a different year?

Thanks

When you query for this:

BETWEEN MONTH(CURDATE() - INTERVAL 9 MONTH) AND MONTH(NOW())

You are querying from month 12 - 9. Because 12 is greater than 9 there will be no results. But when you query for this:

BETWEEN MONTH(CURDATE() - INTERVAL 8 MONTH) AND MONTH(NOW())

You are querying from month 1 - 9.

The solution is that you need to include the year in your query as well, and not just the number of the month.

Have you tried changing the where clause dealing with the month being between the two values? I bet if you change that to dealing with just the pure dates you should be fine.