从MySQL表中获取特定年龄段的人

时间:2015-01-27 07:50:18

标签: php mysql date

嗯,我们说我已经得到了下表:

+----+-----------+--------+------------------+
| id | name      | gender | birthDate (date) |
+----+-----------+--------+------------------+
| 1  | John Doe  | Male   | 1968-09-18       |
| 2  | Jane Doe  | Female | 1970-11-20       |
| 3  | Sally Doe | Female | 1985-04-17       |
| 4  | Jake Doe  | Male   | 1989-07-10       |
| 5  | Mike Doe  | Male   | 1991-06-28       |
| 6  | Cindy Doe | Female | 1995-02-03       |
+----+-----------+--------+------------------+

如何从此表中获得21岁以上和50岁以下的所有人。我通常可以在mysql中找到自己的方式但是当涉及到日期时,我似乎总是陷入困境。

2 个答案:

答案 0 :(得分:0)

select * from your_table
where curdate() - interval 21 year >= birthDate 
and curdate() - interval 50 year < birthDate 

答案 1 :(得分:0)

使用BETWEEN语句的另一种方式。

SELECT *
FROM tablename
WHERE birthdate BETWEEN
(curdate() - interval 50 year)
AND 
(curdate() - interval 21 year)