如果您的演算法取決於該人的出生年份,則顯然存在問題.要解決此問題,首先在當前日期之後找到每個人的下乙個生日,然後計算該日期與現在之間的差異.
select u_birth, datediff(next_birthday, now()) as distance from (
select *, adddate(birthday, interval birthday < date(now()) year) as next_birthday
from (
select *, adddate(u_birth, interval year(now()) - year(u_birth) year) as birthday
from blog_users
where s_agehide = 0
) as t1
) as t2
order by distance asc
limit 5
結果:'1992-02-29', 20
'1993-03-01', 21
'1987-05-15', 96
'1988-05-15', 96
'1988-09-18', 222
測試資料:
create table blog_users (u_birth nvarchar(100) not null, s_agehide int not null);
insert into blog_users (u_birth, s_agehide) values
('1987-05-15', 0),
('1988-05-15', 0),
('1988-09-20', 0),
('2000-01-02', 0),
('2000-01-03', 1),
('1988-09-19', 0),
('1988-09-18', 0),
('1992-02-29', 0),
('1993-03-01', 0);
請注意,假設在閏日出生的人在非閏年有2月28日的生日.
此外,您的查詢不包括使用者的使用者id.你可能想要新增它,我想.
生日提醒 還有多久生日(mysql)
考慮兩點 1.年底時,倒計時 2.閏年2月29日出生的 針對以上,首先獲取今年和來年的生日日期,今年生日過了則計算下一年生日距離今天多少天 閏年2月29生日的,設在非閏年時2月28日過生日,恰巧,mysql預設沒有29號時取28號。select datediff if cur today,cur,n...
mysql 最近過生日
需求 檢視最近20天過生日的客戶 思路 分兩種情況,第一種 客戶月日在現在月日之後,計算今年生日到現在的天數。第二種 客戶月日在現在月日之前,計算明年生日到現在的天數。select from t investor where birthday is not null and case when da...
HR必看!設定員工生日提醒
在提到日期計算時,幾乎所有的excel圖書中都會提到乙個隱藏函式 datedif。這個函式的確挺好用,但令人奇怪的是,這居然是個隱藏函式,你甚至在excel的幫助資訊裡也找不到它。這個函式是用來計算兩個日期之間的間隔時間的,它的語法是 datedif start date,end date,unit...