MySQL + SQL · Lesson 1

Records Last 7 Days

Recent Records

To get rows created in the last 7 days, compare the date column with the date 7 days ago using DATE_SUB.

The Query

SELECT * FROM orders
WHERE created_at >= DATE_SUB(CURDATE(), INTERVAL 7 DAY);

How it Works

DATE_SUB(CURDATE(), INTERVAL 7 DAY) gives the date 7 days ago. Any row with a newer date is from the last week.

Summary

  • Use created_at >= DATE_SUB(CURDATE(), INTERVAL 7 DAY).
  • Change 7 to any number of days you need.
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

\n