Using between mysql function ignoring year

你。 提交于 2019-12-11 08:13:25

问题


Ok, I have the following Users table:

id |  name  |  birthday 

1  |  nam1  |  1980-06-29  
2  |  nam2  |  1997-07-08  
3  |  nam3  |  1997-07-20  

Assuming that today is 2012-06-29

How can I get the users who will celebrate your birthdays in the next 15 days? I have tried with this:

select * from users where birthday between '%-06-29' and '%-07-14';

but looks like is not a valid query.


回答1:


I think the safest way is to compare their age in 15 days with their age today:

SELECT *
FROM   Users
WHERE  TIMESTAMPDIFF(YEAR, birthday, CURRENT_DATE + INTERVAL 15 DAY)
     > TIMESTAMPDIFF(YEAR, birthday, CURRENT_DATE)

See it on sqlfiddle.




回答2:


you should try operating on a substring of "birthday" so you can "cut" the year out of the field



来源:https://stackoverflow.com/questions/11265610/using-between-mysql-function-ignoring-year

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!