开发者

How do I sorting MySQL result by ages from the birthday date

Let's say my MySQL database structure something like this.

ID     Name    BirthdayDate 
------------------------------------- 
1      John   开发者_开发问答 1974-05-21
2      Peter   1977-11-10
3      Chang   1981-04-01
..................
..................
100    Smith   1945-03-21

How to make a SQL statement to sorting age example

40 > 45 above
35 <= below or equal 35

Let me know


ORDER BY IF(BirthdayDate < DATE_SUB(NOW(), INTERVAL 35 YEARS), 0, 1),
         BirthdayDate 

This will show at first the ones, whose age is > 35 years, after that - others


try more simple

SELECT * FROM YOUR_TABLE_NAME 
WHERE TIMESTAMPDIFF(YEAR,BirthdayDate, CURDATE()) > 45

OR

SELECT * FROM YOUR_TABLE_NAME 
WHERE TIMESTAMPDIFF(YEAR, BirthdayDate, CURDATE()) BETWEEN 18 AND 45
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜