mySQL SELECT upcoming birthdays

前端 未结 17 675
我寻月下人不归
我寻月下人不归 2020-12-03 11:17

I\'m trying to write a query to select users of a database whose birthdays are in the next 7 days.

I\'ve done a lot of research but I can\'t come up with a working s

17条回答
  •  醉酒成梦
    2020-12-03 11:55

    So another approach you can take is to do a little more work before hitting the database layer. For example, I recently did this in a Laravel (PHP) and Postgres project. I basically built up an array of dates (ie. the next 7 days), and did a where in query to find users with birthdays on those dates.

    User::whereNotNull('birth_date')
        ->where('birth_date', '<=', $endDate)
        ->whereIn(DB::raw("to_char(birth_date, 'MMDD')"), Carbon::range($startDate, $endDate)->map(function ($date) {
            return $date->format('md');
        }))->get();
    

提交回复
热议问题