0

I'm wondering how to use a variable in a SELECT statement to load data from any month from my SQL server! Currently I have this (the below code), which loads the currently passed month! Is there a way to say that timespan minus 1 month? or 2 months? and then have that negative month number as a variable to use with AJAX? To for instance load 3 months back in time or when you want 4 months back in time, and so on!? It is important that the latest month is from now and one month back in time, and the variable the same time minus the months typed in!

SELECT hrs, snitt_fukt, max_fukt, min_fukt
FROM `testingf` 
where hrs between date_sub(now(),INTERVAL 1 MONTH) and now();
Bluelight
  • 9
  • 4
  • Simply by changing the `1` in `INTERVAL 1 MONTH` to whatever number desired? – sticky bit Dec 18 '19 at 03:19
  • @stickybit So `SELECT hrs, snitt_fukt, max_fukt, min_fukt FROM testingf where hrs between date_sub(now(),INTERVAL 2 MONTH) and date_sub(now(),INTERVAL 1 MONTH);` And then adjust both the first and second month value? – Bluelight Dec 18 '19 at 04:20
  • Yes, if you want the data for the month before the last month. No if you want the data from two months ago until now. Then the second boundary stays `now()`. That wasn't clear in your question. – sticky bit Dec 18 '19 at 04:20
  • @stickybit Yes! It should be a one month at a time view and possibility to look back in time! ;) Now to figure out how to use AJAX for this! Oh, my! – Bluelight Dec 18 '19 at 04:25
  • Is this helpful https://stackoverflow.com/q/7537377/1839439 – Dharman Dec 18 '19 at 07:26
  • Use `CURDATE()` instead of `NOW()` if you want the boundary to be midnight instead of some time midday. – Rick James Dec 19 '19 at 20:09

0 Answers0