Return data within a period of time

Asked

Viewed 31 times

-1

I have the following query:

SELECT raddb.FisioterapiaUtente.Id, DataConsulta, Inicio, Fim, raddb.UtentesCD.Utente, nome  
FROM raddb.FisioterapiaUtente LEFT OUTER JOIN raddb.usuarios ON raddb.usuarios.id = raddb.FisioterapiaUtente.Fisioterapeuta
LEFT OUTER JOIN raddb.UtentesCD ON raddb.UtentesCD.IdUtente = raddb.FisioterapiaUtente.Utente WHERE DataConsulta BETWEEN '2019-10-26' AND '2019-11-25'

I intend to always research this period (between 26 and 25 of each month) over the year, so the problem is to define the dates dynamically.

  • Make a WHERE with a condition in the date column of your record. Here: read about the BETWEEN.

  • @Woss Yes, I know that the problem is how I always define the 26th of the previous month automatically and the 25th of the current month automatically.

  • Um, then I could clarify that in the question. Can [Dit] and put an example of the condition you need to have, even with fixed dates and describe that the problem is only in setting the dates dynamically?

  • @Woss edited the question as you suggested, so it is clear my problem. Thank you

1 answer

1


If we are in month 12, will search between 26/10 and 25/11, as it will get the first date decreasing 2 months from the current and the second date 1 month from the current.

SELECT raddb.FisioterapiaUtente.Id,
       DataConsulta,
       Inicio, 
       Fim, 
       raddb.UtentesCD.Utente, 
       nome  
  FROM raddb.FisioterapiaUtente LEFT OUTER JOIN raddb.usuarios ON raddb.usuarios.id = raddb.FisioterapiaUtente.Fisioterapeuta
                                LEFT OUTER JOIN raddb.UtentesCD ON raddb.UtentesCD.IdUtente = raddb.FisioterapiaUtente.Utente 
 WHERE DataConsulta BETWEEN SUBDATE(concat(year(current_date()),'/',month(CURRENT_DATE()),'/','26'), INTERVAL 2 MONTH) AND
                            SUBDATE(concat(year(current_date()),'/',month(CURRENT_DATE()),'/','25'), INTERVAL 1 MONTH) 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.