MySQL How to SELECT data from table which recorded today?

MySQL How to SELECT data from table which recorded today?

  1. Blog
  2. 15 November 2026
  3. 2 min read

Stefan Bogdanescu

Founder & Senior Architect

Selecting Today's Records in MySQL: Beyond Simple Equality Checks

A common requirement when working with temporal data is filtering records based on the current date. You have a table where a datetime column, populated by the SQL NOW() function, stores values like 2010-10-07 10:57:36. The immediate instinct is often to compare this field directly against the current date string. In your initial attempt, you likely wrote something resembling SELECT * FROM table WHERE date = ???.

The critical issue here is how MySQL handles datetime comparisons versus equality checks. When you use the = operator, MySQL expects an exact match down to the second. If your column contains 2010-10-07 10:57:36 and today is 2024-10-07, a simple string comparison like WHERE date = '2024-10-07' will fail to return any rows. This is because the stored value includes a time component (10:57:36) that does not exist in your filter condition. The database interprets '2024-10-07' as midnight of that day (2024-10-07 00:00:00). Since 10:57:36 is not equal to 00:00:00, the row is excluded.

To retrieve all records where the date portion matches today, you must explicitly define a range that captures the entire day. The most robust and performant approach involves using a range query with a BETWEEN clause or explicit comparison operators. You need to select rows starting from the beginning of today at midnight up to the very last second of today.

You can construct this by generating the start of the day and appending 23 hours, 59 minutes, and 59 seconds for the end point:

SELECT * FROM your_table
WHERE created_at BETWEEN '2024-10-07 00:00:00' AND '2024-10-07 23:59:59';

However, relying on hardcoded dates is not scalable. In a dynamic application, you should generate these boundaries programmatically within your SQL query using MySQL's date functions. You can calculate the start of today by casting the current timestamp to a date and adding zero seconds, then add one day to the start date and subtract one second to get the exclusive upper bound:

SELECT * FROM your_table
WHERE created_at >= DATE_FORMAT(NOW(), '%Y-%m-%d 00:00:00')
  AND created_at < DATE_FORMAT(NOW() + INTERVAL 1 DAY, '%Y-%m-%d 00:00:00');

This logic ensures that the query automatically adapts to whatever "today" is at execution time without requiring manual updates to the SQL statement.

Indexing Considerations and Performance

While the BETWEEN or range syntax works perfectly for correctness, it is crucial to consider how this affects database indexing. If your created_at column is indexed (which is standard practice for datetime columns), a simple equality check like WHERE created_at = '...' efficiently uses that index. A range query also utilizes the index effectively because B-Tree indexes support range scans.

However, avoid using functions directly on the column in your WHERE clause, such as WHERE DATE(created_at) = CURDATE(). This forces MySQL to evaluate the function for every single row before checking the index, resulting in a full table scan and significantly degraded performance on large datasets. By comparing the raw datetime value against calculated boundaries, you allow the optimizer to use the existing index without evaluating functions on the column data itself.

Laravel Integration

If you are building this application using the Laravel framework, the syntax abstracts away the raw SQL construction while maintaining these best practices. You can use Eloquent's whereDate method or a raw query builder that encapsulates the logic described above. For instance, filtering for today becomes:

$todayRecords = YourModel::where('created_at', '>=', now()->startOfDay())
                         ->where('created_at', '<', now()->endOfDay())
                         ->get();

This approach keeps your code clean and ensures that the underlying SQL generated respects the indexing strategies discussed. Whether writing raw SQL or using an ORM, the principle remains the same: compare the full datetime against a calculated range to include the time component inherent in NOW().

Our network