Jump to content

[SOLVED] how to query age.. between 18y/o and 22y/o


DanDaBeginner

Recommended Posts

:) what I did is to get the $fromAge and $toAge, I did this with PHP..compute the year then insert to query..ex.

 

$tAge = date("Y") - 18; //1989

$fAge = date("Y") - 22; //1985

 

$query = "

SELECT *  from dating_profile

WHERE EXTRACT(YEAR FROM birthdate)

BETWEEN $fAge and $tAge "

and it works fine.. im just wondering how to do it in mysql query instead of computing the $tAge and $fAge..

if I will do this in mysql query will it be faster than computing 1st the  $tAge and $fAge then query?

 

thanx

Dan

Link to comment
Share on other sites

I suppose extract(year from birthdate) would be faster than round(datediff(CURRENT_DATE, birthdate) / 365) and if the following is correct than extracting the date is substantially quicker:

DROP TABLE IF EXISTS birthdate;

CREATE TABLE birthdate (
id int(11) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
birthdate DATE NOT NULL 
)TYPE=MYISAM;

INSERT INTO birthdate SET birthdate = '1974-12-03';
INSERT INTO birthdate (birthdate) SELECT adddate(birthdate, 1) FROM birthdate; # repeated a number of times

SELECT count(*) FROM birthdate; # records created 2097152

SELECT count(round(datediff(CURRENT_DATE, birthdate) / 365)) AS test FROM birthdate; # average around 922 ms

SELECT count(extract(year from birthdate)) AS test FROM birthdate; # average around 328 ms

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.