Tuesday, October 30, 2012

MySQL select today and last 30 days records


If you need to select records from MySQL table records with date today and last 30 days records.
based on the Server date.

MySQL select today and last 30 days records


SELECT * FROM posts WHERE 
date_of_create BETWEEN CURDATE() - INTERVAL 30 DAY AND CURDATE();
In case if your field data type were DATETIME format you need to convert your datetime format into date
SELECT DATE_FORMAT(date_of_create, '%m/%d/%Y') FROM posts
WHERE   date_of_create BETWEEN CURDATE() - INTERVAL 30 DAY AND CURDATE()

1 comment:

  1. Another option is 'where date_of_create > DATE_SUB( CURDATE(), INTERVAL 30 DAY)'

    ReplyDelete