1
get last x days
CGuild1.com > Tips & Tricks > get last x days

Here's how to Query By Last Month, Last Week, Yesterday, And So On

You can use DATE_SUB:


For Last Month

SELECT * FROM table WHERE YEAR(date_created) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH) AND MONTH(date_created) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)

For Last 30 Days

WHERE order_date > DATE_SUB( NOW(), INTERVAL 1 MONTH )

For Last Week

WHERE order_date > DATE_SUB( NOW(), INTERVAL 1 WEEK ) For Today

WHERE order_date > DATE_SUB( NOW(), INTERVAL 1 DAY ) For in Between Dates

WHERE order_date BETWEEN ( DATE_SUB( NOW(), INTERVAL 2 DAY ), DATE_SUB( NOW(), INTERVAL 1 DAY ) )

Original source on Stackoverflow
©2024 CGuild1.com