Reputation: 29
I have a table consisting reports, I want to select data of last 7 day's. So in below image you can see 3 column day's month year.
What is the sql query to select last 7 day's.
Upvotes: 0
Views: 74
Reputation: 1368
try something like this,
SELECT
*
FROM
< your_table >
WHERE
STR_TO_DATE(CONCAT(`day`, ',', `month`, ',' ,`year`), '%e,%m,%y')
BETWEEN
DATE_SUB(CURDATE(), INTERVAL 7 DAY) AND CURDATE();
If you want to use latest date from the same table,
try fetch max date separately with query ,
SELECT MAX(STR_TO_DATE(CONCAT(`day`, ',', `month`, ',' ,`year`), '%e,%m,%y')) AS maxdat FROM < your_table >
store it, and use it in the main query in-place of CURDATE(),
or,
you can use the same query as a sub-query if you don't have any performance issues.
Upvotes: 1
Reputation: 1907
On which basis you want last 7 days entry.
Try this to find date before 7 days:
WHERE t.date >= DATE_ADD(CURDATE(), INTERVAL -7 DAY);
OR
WHERE t.date >= DATE(NOW()) - INTERVAL 7 DAY
Or you can go through Date_doc documentation.
Upvotes: 0