MYSQL根据年龄范围选择生日 [英] MYSQL select birthday based on age range

查看:40
本文介绍了MYSQL根据年龄范围选择生日的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的 MYSQL 表,我在其中存储数据类型为 date 的生日.行:

I have a simple MYSQL table where I store birthday with the datatype date. Rows:

birthday
--------
1989-08-08

我想选择年龄在 18 到 25 岁之间的用户.我使用以下查询:

I want to select users with age between 18 and 25 years. I use the following query:

SELECT * FROM users 
WHERE birthday <= now() - INTERVAL 18 YEAR and
      birthday >= now() - INTERVAL 25 YEAR 

这应该返回上面的行,因为它在 18 到 25 之间的范围内,但 MYSQL 假定它是 26 岁,因为它在 25 到 26 之间

This should return the above row as It fits in the range between 18 and 25 but MYSQL assumes that it's 26 years old as it's between 25 and 26

推荐答案

25 年前是 1989-09-27 并且 1989-08-08 小于 1989-09-27 所以它不会被返回.如果您想要目前 25 岁但尚未 26 岁的人,请更改为 INTERVAL 26 year + INTERVAL 1 DAY;

25 years ago was 1989-09-27 and 1989-08-08 is less than 1989-09-27 so it will not be returned. If you want people who are currently 25 but not yet 26, change to INTERVAL 26 year + INTERVAL 1 DAY;

这篇关于MYSQL根据年龄范围选择生日的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆