我在MySQL数据库表中有一个BirthDate列来存储用户的出生日期。现在我在html / php中有两个字段(1。年龄从2.年龄到)。
如果用户想要让所有年龄介于10岁到20岁之间的用户,是否可以使用BirthDate列进行MySQL查询。
由于
答案 0 :(得分:9)
您的查询与此类似:
SELECT * FROM your_table WHERE YEAR(CURDATE())-YEAR(BirthDate) BETWEEN 10 AND 20;
答案 1 :(得分:8)
你可以这样做
SELECT column1, column2,
DATE_FORMAT(FROM_DAYS(TO_DAYS(now()) - TO_DAYS(dob)), '%Y') + 0 as age
WHERE age >10 AND age <20 ;
答案 2 :(得分:5)
select * from table where
(year(curdate())-year(dob))-(right(curdate(),5)< right(dob,5) )
between 10 and 20
答案 3 :(得分:2)
另一种检查年份和月份的解决方案:
SELECT * FROM yourTable WHERE FLOOR(DATEDIFF(curdate(), birthdate) / 365.25) BETWEEN 10 AND 20
答案 4 :(得分:0)
也可以使用子查询来实现:
select *
from (select *, TIMESTAMPDIFF(YEAR, birthday, NOW()) as age from table_name) as sub_query
where age between 10 AND 20