2

I have birth date data stored in Mysql table as below:

id firstname middlename lastname   birth
---------------------------------------------
 1 Harry     Chandra    Sihombing  1999-12-14
 2 Janice    Mona       Sihombing  2010-04-20
 .  ...      ...        ...        ...

How can I create a search function to collect data from the table and restrict the output to rows "where age > 25" (and/or other function using ">" (greater) "=" (equal).

2 Answers 2

1

maybe something like this with DATE_SUB

SELECT * FROM people WHERE DATE_SUB(NOW(),INTERVAL 25 YEAR) > birth

in php:

$age = (int)$_POST['age'];
$sql = "SELECT * FROM people WHERE DATE_SUB(NOW(),INTERVAL $age YEAR) > birth";
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, your script doing right. but i want using number interval form variable passing by field search.
the script become: DATE_SUB(NOW(),INTERVAL $search YEAR) < birth I'm get this error: You have an error in your SQL syntax; Note: $search are value submitted from input text.
0

Here's a hint:

SELECT NOW(),
    DATE(NOW()),
    DATE_SUB(NOW(), INTERVAL 25 YEAR),
    DATE(DATE_SUB(NOW(), INTERVAL 25 YEAR))

... renders something like:

+---------------------+-------------+-----------------------------------+-----------------------------------------+
| NOW()               | DATE(NOW()) | DATE_SUB(NOW(), INTERVAL 25 YEAR) | DATE(DATE_SUB(NOW(), INTERVAL 25 YEAR)) |
+---------------------+-------------+-----------------------------------+-----------------------------------------+
| 2019-10-29 10:27:52 | 2019-10-29  | 1994-10-29 10:27:52               | 1994-10-29                              |
+---------------------+-------------+-----------------------------------+-----------------------------------------+

(Fiddle).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.