Home > Database > Mysql Tutorial > body text

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

Mary-Kate Olsen
Release: 2024-10-24 03:14:01
Original
835 people have browsed it

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')
Copy after login

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!

source:php
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!