Adam Perinay
Adam Perinay

Reputation: 35

MySQL from last specific month

I have database with toners. I need to get result only from lastest spetember every year to currrent date.

SELECT * FROM used_toners WHERE date >= '01-09-2018' # but after september of 2019 I need date '01-09-2019' and so on

Upvotes: 2

Views: 587

Answers (2)

Alberto Moro
Alberto Moro

Reputation: 1013

With this query I select all the ranges of dates from September of last year to September of this year.

SELECT *
FROM used_toners
WHERE (YEAR(`date`) = YEAR(DATE_SUB(CURDATE(), INTERVAL 1 YEAR))
    AND MONTH(`date`) BETWEEN 9 AND 12)
OR (YEAR(`date`) = YEAR(CURRENT_DATE())
    AND MONTH(`date`) BETWEEN 1 AND 9)

Here you can find MONTH() and YEAR() documentation.

Upvotes: 1

Gordon Linoff
Gordon Linoff

Reputation: 1271151

If you want data from all Septembers, you can use month() or similar functions:

SELECT ut.*
FROM used_toners ut
WHERE MONTH(date) = 9;

Upvotes: 1

Related Questions