How to get Age using BirthDate column by MySQL query?

前端 未结 5 1971
滥情空心
滥情空心 2021-01-19 02:56

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 t

相关标签:
5条回答
  • 2021-01-19 03:14
    select * from table where
    (year(curdate())-year(dob))-(right(curdate(),5)< right(dob,5) )
    between 10 and 20
    
    0 讨论(0)
  • 2021-01-19 03:17

    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 ; 
    
    0 讨论(0)
  • 2021-01-19 03:18

    Another solution which checks the year and the month:

    SELECT * FROM yourTable WHERE FLOOR(DATEDIFF(curdate(), birthdate) / 365.25) BETWEEN 10 AND 20
    
    0 讨论(0)
  • 2021-01-19 03:19

    Your query would look similar to this:

    SELECT * FROM your_table WHERE YEAR(CURDATE())-YEAR(BirthDate) BETWEEN 10 AND 20;
    
    0 讨论(0)
  • 2021-01-19 03:24

    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 
    
    0 讨论(0)
提交回复
热议问题