Jump to content

[SOLVED] filtering according to age and DOB


stephenk

Recommended Posts

I have an age range coming in as a POST variable in the form a-b where a is the lower boundary and b is the upper one.

In my database, the DOB of various people are stored.

Can anyone tell me the BETWEEN query to select the people that are within the age range $lower and $upper, according to their date of birth?

 

I really hate working with dates!

 

Many thanks,

Stephen

Link to comment
Share on other sites

(I'll assume you've been able to check the POST data for proper input and separated it into $lower and $upper.)

 

$query = "SELECT * FROM people_table WHERE dob_col BETWEEN CURDATE() - INTERVAL $upper YEAR AND CURDATE() - INTERVAL $upper YEAR";

Link to comment
Share on other sites

Thanks for your help.

 

However I'm running this query:

SELECT *
FROM tblpeople
WHERE `dob`
BETWEEN CURDATE( ) - INTERVAL 1 YEAR
AND CURDATE( ) - INTERVAL 10 YEAR

 

There are people in the table with DOBs ranging from 1983-05-05 to 2002-09-07 and the query isn't returning any records.

 

Stephen

 

Edit... Got it: I had got the upper and lower switched round - In your query above you had upper twice and I changed the wrong one.

 

Thanks for your help!!

Link to comment
Share on other sites

Switch the numbers.  CURDATE() - INTERVAL 1 YEAR is going to be a later year than CURDATE() - INTERVAL 10 YEAR; you need to put the earlier year first.  Look where I put $upper and $lower in the query above.

 

mysql> SELECT CURDATE() - INTERVAL 10 YEAR, CURDATE() - INTERVAL 1 YEAR,'2002-09-07' BETWEEN CURDATE() - INTER
VAL 10 YEAR AND CURDATE() - INTERVAL 1 YEAR AS "Between?";
+------------------------------+-----------------------------+----------+
| CURDATE() - INTERVAL 10 YEAR | CURDATE() - INTERVAL 1 YEAR | Between? |
+------------------------------+-----------------------------+----------+
| 1997-06-25                   | 2006-06-25                  |        1 |
+------------------------------+-----------------------------+----------+
1 row in set (0.00 sec)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.