MySQL Query - How to select records of today to next Sunday ?

December 18, 2017 | Category : MySQL

In this tutorial, i will let you know how to get records of today to weekend by using mysql query. This is MySQL Query that you can easily use in your core PHP and PHP framework like Laravel, Codeigniter, cakephp etc project.

If you require to get records this way, today is Tuesday and you need to get data from today to next Sunday that means Tuesday to Sunday. sometimes we require to fetch records from this way. It is not any predefine function for getting records this way but we can do it by using following MySQL pre-define function.

1) date()

2) now()

3) INTERVAL

4) weekday()

5) DAY

6) CURRENT_TIMESTAMP

So, Basically we will fetch data by using above pre-define function of MySQL. In this example i have one "users" table and there are several fields. I will get only today to next Sunday records by compare with created_at column. So you can also use this mysql query on your php project. So, let's see bellow mysql query:

MySQL Query

SELECT *, date(now() + INTERVAL 6 - weekday(now()) DAY) as this_sunday_date

FROM `users`

WHERE created_at <= date(now() + INTERVAL 6 - weekday(now()) DAY)

AND created_at >= CURRENT_TIMESTAMP

This query is very small but it can help you more.