How to get Age using BirthDate column by MySQL que

2019-02-22 13:16发布

I have a BirthDate column in MySQL database table to store user's date of birth. Now I have form in html/php with two fields(1. Age From 2. Age To).

If user want to get all users where their ages are between 10 years and 20 years, Is it possible to do this with MySQL query using BirthDate column.

Thanks

5条回答
一夜七次
2楼-- · 2019-02-22 13:35

You can get like this

SELECT column1, column2, 
   DATE_FORMAT(FROM_DAYS(TO_DAYS(now()) - TO_DAYS(dob)), '%Y') + 0 as age     
WHERE age >10 AND age <20 ; 
查看更多
Animai°情兽
3楼-- · 2019-02-22 13:41

It can also be achived by using a sub query:

select *
    from (select *, TIMESTAMPDIFF(YEAR, birthday, NOW()) as age from table_name) as sub_query
    where age between 10 AND 20 
查看更多
爷、活的狠高调
4楼-- · 2019-02-22 13:57
select * from table where
(year(curdate())-year(dob))-(right(curdate(),5)< right(dob,5) )
between 10 and 20
查看更多
smile是对你的礼貌
5楼-- · 2019-02-22 13:59

Your query would look similar to this:

SELECT * FROM your_table WHERE YEAR(CURDATE())-YEAR(BirthDate) BETWEEN 10 AND 20;
查看更多
贼婆χ
6楼-- · 2019-02-22 14:02

Another solution which checks the year and the month:

SELECT * FROM yourTable WHERE FLOOR(DATEDIFF(curdate(), birthdate) / 365.25) BETWEEN 10 AND 20
查看更多
登录 后发表回答