Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning Rows from MySql less than 60 Days Old

I have the following query returning all rows from my table:

$query="SELECT * FROM $tbl_name ORDER BY job_id DESC";

I'd like to limit those results by entries less than 60 days old. I record the date an entry was made to the database using:

$dt=date('d M Y');

And this is stored in a column called 'date'.

Can someone help me to modify my query?

Thanks Dan

like image 979
Dan Avatar asked Oct 25 '25 18:10

Dan


2 Answers

If date is stored like a varchar in database, your query should be:

SELECT * 
FROM $tbl_name 
WHERE TO_DATE(date, 'dd MON yyyy') >= DATE_SUB(CURDATE(), INTERVAL 60 DAY)
ORDER BY job_id DESC

if date is stored like a date, use:

SELECT * 
FROM $tbl_name 
WHERE date >= DATE_SUB(CURDATE(), INTERVAL 60 DAY)
ORDER BY job_id DESC
like image 164
Andrey Andrei Avatar answered Oct 28 '25 08:10

Andrey Andrei


    $query="SELECT * FROM $tbl_name WHERE DATEDIFF(CURDATE(), STR_TO_DATE(date,'%d %M %Y')) <60 ORDER BY job_id DESC"
like image 39
Daniel Gruszczyk Avatar answered Oct 28 '25 09:10

Daniel Gruszczyk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!