[英]Mysql Date Field Search using months to find upcoming birthday
I am trying to fins 'upcoming birthdays' of the users present in the Database. 我正在尝试寻找数据库中存在的用户的“即将来临的生日”。
I can able to fine the users upcoming birthday only till 12 month (Dec). 我只能将用户即将到来的生日罚款至12个月(12月)。
What if I need to find the upcoming birthday of next year. 如果我需要找到明年的生日该怎么办。
My Query: 我的查询:
SELECT c.F_CONTACT_ID,
c.F_CONTACT_NAME,
c.F_CONTACT_FNAME,
DATE_FORMAT(c.F_DOB, '%d/%m') F_DOB
FROM t_contact c
WHERE DATE_FORMAT(F_DOB, '%m') >= '08'
GROUP BY c.F_CONTACT_ID
ORDER BY c.F_DOB ASC LIMIT 0 ,
10
How can I find the upcoming birthday of the users who born on 'January' because the 我如何找到出生于“一月”的用户的即将到来的生日,因为
DATE_FORMAT(F_DOB, '%m') >= '08'
DATE_FORMAT(F_DOB,'%m')> ='08'
can able to check till 12th month. 可以检查到12个月。
Can some one help me? 有人能帮我吗?
I have no idea why you have a group by
. 我不知道为什么你有一个
group by
。 I would do this with an order by
and limit
: 我会通过
order by
和limit
来做到这一点:
SELECT c.F_CONTACT_ID,
c.F_CONTACT_NAME,
c.F_CONTACT_FNAME,
DATE_FORMAT(c.F_DOB, '%d/%m') F_DOB
FROM t_contact c
ORDER BY (case when month(c.F_DOB) >= '08' then month(c.F_DOB)
else month(c.F_DOB) + 12
end)
LIMIT 0, 10;
That is, add "12" to the month numbers for next year. 即,将“ 12”添加到明年的月份号中。 Note: this doesn't take the day of the month into account because your original query does not.
注意:这不会考虑月份中的某天,因为您的原始查询没有。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.