Skip to content
Advertisement

MySql select all records from the table with month and year in the range from that month and year to all the past [closed]

I have a table with two fields “month” and “year” how can I make a query that, passing a month and a year, select everything from that month and year back. For example, if the month and year in the query are 9 for the month and 2020 for the year, if I have records in the table that are from the same month and year or two previous months or one year or more in the past, do they all go out?

Thank you all

Advertisement

Answer

One option uses arithmetics:

select *
from mytable
where year * 100 + month <= date_format(current_date, '%Y%m')

Or:

where year * 100 + month <= year(current_date) * 100 + month(current_date)
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement