Home >Database >Mysql Tutorial >How to Use MySQL to Find Users with Birthdays on a Given Date?

How to Use MySQL to Find Users with Birthdays on a Given Date?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-10-24 03:14:01909browse

How to Use MySQL to Find Users with Birthdays on a Given Date?

Using MySQL to Find Birthdays on a Specific Date

You have a database table containing user information, including their birthdays stored as UNIX timestamps. To automate birthday email notifications, you need a MySQL query that identifies users whose birthdays fall on the current day.

To achieve this, you can use the following query:

SELECT * 
FROM USERS
WHERE 
   DATE_FORMAT(FROM_UNIXTIME(birthDate),'%m-%d') = DATE_FORMAT(NOW(),'%m-%d')

Explanation:

  • DATE_FORMAT(FROM_UNIXTIME(birthDate),'%m-%d') converts the birthdate stored in UNIX timestamp format to a string in the format 'mm-dd'.
  • DATE_FORMAT(NOW(),'%m-%d') gets the current date in the same 'mm-dd' format.
  • The WHERE clause compares the two date strings, identifying rows where the user's birthday matches today's date.

The above is the detailed content of How to Use MySQL to Find Users with Birthdays on a Given Date?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn