Sam Sam - 3 months ago 10
MySQL Question

Get all data between months in a span of years

Sorry so this is something I've been trying to figure out for a while now and can't seem to get.

I have a database with a column, Month, and I want to get all of the months between November and May of the next year between 1990 and 2010. This is something where I'm just at a complete loss of where to go and I've tried using:

DATE_ADD(Month, INTERVAL 6 MONTH) AND YEAR(Month) BETWEEN 1990 AND 2010


With no luck.

The examples I've been seeing are people who want to use
CURDATE
which isn't the case here.

Answer

How about this:

WHERE (MONTH(date) >= 11 AND YEAR(date) BETWEEN 1990 AND 2009) OR
      (MONTH(date) <= 5  AND YEAR(date) BETWEEN 1991 AND 2010)

This assumes that you have a column called date which is one of MySQL's date types. The first condition in the WHERE clause matches November and December of 1990 (but not the spring of that year), as well as November/December of all years up until 2009. The second condition matches January-May of 1991 up to and including 2010.