I am creating a report where I have to show last 7 days data and previous 7 days data. But I can fetch last 7 days data but for previous 7 days I don’t know how to write the sql!
Suppose today is 14th March. I am fetching data from database for last 7 days which is 8th March to 14th March is,
WHERE my_date >= DATE_ADD(NOW(), INTERVAL -7 DAY)
But how to write the sql for previous 7 days? Which is 1st March to 7th March.
I have tried this,
WHERE my_date BETWEEN DATE_SUB(NOW(),INTERVAL 7 DAY) and NOW()
I don’t think it’s working! How am I going to get data for 1st March to 7th March and skip 8th March to 14th March?
Have you try this :
WHERE my_date BETWEEN DATE_SUB(NOW(),INTERVAL 14 DAY) and DATE_SUB(NOW(),INTERVAL 7 DAY)
Answered By – Alaindeseine
Answer Checked By – Mildred Charles (BugsFixing Admin)