How would I get the birthdays of friends who are celebrating their birthday this week, this month and next month using MYSQL and PHP?

前端 未结 2 1434
旧巷少年郎
旧巷少年郎 2020-12-10 23:48

I am currently working in an site which needs to get the birthdays of friends who are celebrating their birthday this week, this month and next month using MYSQL and PHP.

2条回答
  •  鱼传尺愫
    2020-12-11 00:04

    SELECT  *
    FROM    users
    WHERE   birthday + INTERVAL YEAR(CURRENT_DATE) - YEAR(birthday) YEAR BETWEEN CURRENT_DATE AND CURRENT_DATE + INTERVAL 3 DAY
    

    Note that this condition is not sargable and requires full indes scan.

    In SQL Server, Oracle and PostgreSQL this can be improved using this approach:

    • Selecting birthdays

    , but MySQL lacks a way to generate a random resultset.

    However, you can create a dummy table and store years from 1900 to 2100 in it.

    Add a cron task to update it in 2100 since you can forget easily :)

提交回复
热议问题