使用MySQL判断今天是否是用户生日
我将所有用户的生日存储为 UNIX 时间戳,并且希望每天向当天生日的用户发送电子邮件。
我需要进行一个 MySQL 查询,该查询将获取包含今天生日的所有行。
看起来这应该相当简单,但也许我只是把它复杂化了。
I have all users' birthdays stored as a UNIXtimestamp and am wanting to send out e-mails each day to users that have a birthday that day.
I need to make a MySQL query that will get all of the rows that contain a birthday on today's date.
It seems like this should be fairly simple, but maybe I am just overcomplicating it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
这应该有效:
This should work:
这是一个答案,属性考虑了闰年,并且总是会为您提供生日在 2 月 29 日与 3 月 1 日同一时间的用户。
Here is an answer that property takes into account leap-years and will always give you the users whose birthday is on the 29th of February at the same time as those on the 1st of March.
如果出生日期存储在表中,您可以使用下面的查询。
今天生日:
昨天生日:
明天生日:
You can use the query below if date of birth stored in a table.
Today Birthday :
Yesterday Birthday:
Tomorrow Birthday:
由于这越来越成为一个代码高尔夫问题,以下是我解决此问题的方法,包括处理闰年:
说明:第一个 where 子句检查某人的生日是否是今天。
第二个确保仅当当前日期等于 2 月最后一天时才选择生日为 2 月 29 日的人。
示例:
Since this gets more and more to be a code-golf question, here's my approach on solving this including taking care of the leap years:
Explanation: The first where clause checks if somebody's birthday is today.
The second makes sure to only select those whose birthday is on Feb 29th only if the current day equals the last day of February.
Examples:
这应该涵盖闰年情况,并使用内部日期机制。
基本上,它的工作原理是将两个日期之间的年份添加到出生日期,并检查与当前日期是否相等:
测试:
This should cover the leap year cases, and uses the internal date mechanics.
Basically it works by adding the years between the two dates to the date of birth and checks for equality with the current date:
Testing:
我遇到了这个问题,我只是使用了这个简单的代码使用
NOW();
结果是今天的生日,所以之后我在用户的生日那天向他们发送电子邮件。
我只使用 DATE 来存储用户的生日,所以我总是有 yy:mm:dd ,所以这就像一个魅力,至少对我来说,使用这种方法。
I come across with this problem, and I just used this simple code using the
NOW();
The results are today's birthdays so after that I working in sending emails to my users on their birthday.
I store my users bithdays using just the DATE so I always have
yy:mm:dd
, so this works like a charm, at least to me, using this approach.我采纳了Saggi Malachi的答案,并把2月29日的生日延长到2月28日,如果那一年没有这样的日子的话。
I took Saggi Malachi's answer and extended to include a birthday on 29th February into 28th February date, if in that year there is no such day.
下面的答案实际上不起作用。它没有考虑到一年有 365.24 天(偶尔有闰日)这一事实,因此与用户出生日期的实际比较至少可以说是复杂的。由于历史原因我要离开它。
The answer below doesn't actually work. It doesn't take into account the fact that a year is 365.24 (leap days now and then) days long, so the actual comparison against the users birthdate is complicated to say the least. I'm leaving it for historical reasons.
这是我的贡献
自 3 月 1 日起,位 '(dayofyear(date_format(current_date,'%Y-03-01'))-60)' 在闰年返回 1,为年份数字 61,在平年返回 0。
从这里开始,只需在“这是我的生日”计算中减去额外的一天即可。
Here's my contribution
The bits '(dayofyear(date_format(current_date,'%Y-03-01'))-60)' returns 1 on leap years since march 1st will be dayofyear number 61, and 0 on normal years.
From here it's just a matter of substracting that extra day to the "is-it-my-birthday"-calculation.
享受吧:)
这会给你一个根据当年计算的人的生日。
然后你就可以用它来进行其他计算!
提供
isBirthday28Feb
和isThisYearLeap
列只是为了说明解决方案。Enjoy :)
This gives you a person's birthday calculated according the current year.
Then you can use it for other calculations!
The columns
isBirthday28Feb
andisThisYearLeap
are given just to illustrate the solution.我采纳了 Saggi 的答案,并考虑修改它,以便它可以显示接下来 7 天的生日,并注意到它还巧妙地解决了闰年问题:)
请注意,它不包括上限。对于闰年,“02-28”和“02-29”之间没有任何内容,但对于非闰年,“02-29”在“02-28”和“03-01”之间。
If you'd want the next 7 days, use:
I took Saggi's answer and thought about modifying it so that it would show birthdays for next 7 days and noticed it also neatly solves the leap year problem :)
Note it doesn't include the upper bound. For leap years nothing falls between '02-28' and '02-29', but for non-leap years the '02-29' falls between '02-28' and '03-01'.
If you'd want the next 7 days, use:
选择 *
来自用户
在哪里
DATE_FORMAT((出生日期),'%m-%d') = DATE_FORMAT(NOW(),'%m-%d')
SELECT *
FROM user
WHERE
DATE_FORMAT((birthDate),'%m-%d') = DATE_FORMAT(NOW(),'%m-%d')
您不能只选择与当天日期匹配的所有行吗?您还可以使用 FROM_UNIXTIME() 函数将 unix 时间戳转换为日期:
mysql>选择 FROM_UNIXTIME(1196440219);
-> '2007-11-30 10:30:19'
这是记录自 http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_from-unixtime
Couldn't you just select all rows that matched the current day's date? You could also use the FROM_UNIXTIME() function to convert from unix timestamp to Date:
mysql> SELECT FROM_UNIXTIME(1196440219);
-> '2007-11-30 10:30:19'
This is documented from http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_from-unixtime
简单的方法
Simple way
您当前正在做的是使用sql确定今天是否是用户生日,如果是,则单独发送愿望,有更好的方法来处理这个问题。
至少只需要两个包含愿望详细信息(日期、姓名、电子邮件)和配置文件 (application.properties) 的 excel 文件,就是这样,您就可以开始了。
此外,还有各种本地运行应用程序的选项(命令行、前台、后台) 、docker、windows 调度程序、unix cron 等)云。
应用程序高度可配置,您可以配置各种详细信息,例如:
其他应用程序级别配置,例如何时发送愿望、迟来的愿望、日志记录等。
免责声明:我是应用程序的所有者
What you are doing currently is determine if today is users birthday using sql and if yes send the wish separately, there is better approach to handling this.
At minimal it just need two things excel file with wish details (Date, name, email) and a configuration file (application.properties) and that is it, you are good to go.
Further there various options to run the application locally (Command line, foreground, background, docker, windows scheduler, unix cron etc) Cloud.
Application is highly configurable , you can configure various details like:
Other application level configurations like, when to send wish, belated wish, logging etc.
Disclaimer : I am the owner of the application