Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how SELECT people within age range [duplicate]

Tags:

select

mysql

pdo

I've already tried all other questions/solutions, and no answer. so here it is: I need to SELECT * FROM people WHERE (dob is 18 to 40)

but my dob is stored as DATE type YYYY-MM-DD

need to select people between 18 and 40 for example!


1 Answers

You need to use BETWEEN with some year calculation TIMESTAMPDIFF(YEAR,'1980-02-04',NOW())

SELECT * FROM people WHERE TIMESTAMPDIFF(YEAR,`dob`,NOW()) BETWEEN  18 AND 40

See fiddle here

TIMESTAMPDIFF

YEAR(date)

like image 82
M Khalid Junaid Avatar answered Sep 17 '25 03:09

M Khalid Junaid