mysql – How would the query be to return values ​​in a given range?

Question:

I have a table with the following rows :

  • code (int)
  • vaccine (varchar)
  • dt_expiration (datetime)

I need a query to return all vaccines that will expire within 10 days, counting today's date.

I tried something like this that I saw in this SOen answer using INTERVAL :

SELECT * FROM tblVacinas WHERE dt_vencimento >= DATE_ADD(CURDATE(), INTERVAL 10 DAY);

Note: it didn't work.

How would the query be to return values ​​in a given range counting from the current date?

Answer:

The problem with your query is that in this part >= DATE_ADD(CURDATE(), INTERVAL 10 DAY) you say to search for dates that are greater than or equal to today's date plus 10 days, for example: on 01/23/2017 your query it will only look for dates beyond 02/02/2017.

If you want to search for a break, just use BETWEEN :

SELECT * 
FROM tblVacinas
WHERE dt_vencimento BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 10 DAY);
Scroll to Top
AllEscort