Jump to content

select date between a range


starphp

Recommended Posts

Hello,

 

I have stored date (birth date in date format).. to find records in between age range, I wrote the following:

 

SELECT * FROM users WHERE brith_date BETWEEN (DATE_SUB (CURDATE(), INTERVAL 20 Year)) AND (DATE_SUB (CURDATE(), INTERVAL 30 Year))

 

It returns a MySql error while I run the query using php mysql_query().. But when I run it in phpmyadmin, there is no error.. Please help me to fix this.

 

Thank You

Link to comment
Share on other sites

 

I have corrected the field name to birth_date .. But still getting the errors on php:

 

New Query is:

 

SELECT * FROM user_details WHERE birth_date BETWEEN (DATE_SUB (CURDATE(), INTERVAL 20 Year)) AND (DATE_SUB (CURDATE(), INTERVAL 30 Year))

 

 

Error:

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')) AND (DATE_SUB (CURDATE(), INTERVAL 31 Year))' at line 18

 

Link to comment
Share on other sites

Thank you for your suggestion.

 

I have added single quotes and now there is no error while it running in php. Updated query is:

 

SELECT * FROM user_details WHERE birth_date BETWEEN '(DATE_SUB (CURDATE(), INTERVAL 20 Year))' AND '(DATE_SUB (CURDATE(), INTERVAL 30 Year))'

 

Link to comment
Share on other sites

 

Query has no errors.. But it doesn't return any record even though, there are number of records between the specified age range..

 

Please help me to fix it.. I am trying to find the records within a age range: 20 to 30

 

SELECT * FROM user_details WHERE birth_date BETWEEN '(DATE_SUB (CURDATE(), INTERVAL 30 Year))' AND '(DATE_SUB (CURDATE(), INTERVAL 20 Year))'

 

Link to comment
Share on other sites

are you sure you can do a function within a command? (sorry noob myself)

 

also you failed to switch 20 with 30 in the last post

 

and you are manipulating/calculating the same value/variable twice, in php at least that is not possible unless you make a reference, I don't know wether that is acceptable in sql (I'd also have to check the manual for that)

Link to comment
Share on other sites

DATE_SUB returns a DATE string, the single-quotes and the extra () are unnecessary -

 

SELECT * FROM user_details WHERE birth_date BETWEEN DATE_SUB (CURDATE(), INTERVAL 30 Year) AND DATE_SUB (CURDATE(), INTERVAL 20 Year)

 

If that does not work, I would suggest that your birth_date column is not what you think it is. Post your table definition and an example of what is in birth_date.

 

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.