How would the query return values in a given interval?

1

I have a table with the following rows :

  • code (int)
  • vaccine (varchar)
  • dt_volution (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 answer SOen using INTERVAL :

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

Note: did not work.

How would the query be to return values at a given interval counting from the current date?

    
asked by anonymous 23.01.2017 / 16:58

2 answers

0
SELECT * FROM tblVacinas WHERE dt_vencimento >= DATE_SUB(curdate(), INTERVAL 10 DAY);

Try using the DATE_SUB () command;

Reference: link

I hope it helps.

    
23.01.2017 / 20:32
0

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 the date of today plus 10 days, example: on 23/01/2017 your query will only fetch dates beyond 02/02/2017.

If you want to fetch a range, just use BETWEEN :

SELECT * 
FROM tblVacinas
WHERE dt_vencimento BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 10 DAY);
    
23.01.2017 / 22:32