0

This is my query to search the data base for candidates who meet a certain criteria. I am using php, mysql, html

$Odata = mysql_query("SELECT * FROM p_candidate WHERE(`gender` LIKE '%".$Gender."%')
     AND (`verbal`  LIKE '%".$Verbal."%') AND(`waiver`  LIKE '%".$Waiver."%')
     AND(`waiver_type`  LIKE '%".$W_Type."%') AND(`sel_staff`  LIKE '%".$A_Staff."%')
     AND(`sel_peers` LIKE '%".$A_Peers."%')AND(`verbal`  LIKE '%".$Awake."%')
     AND(`ambulatory`  LIKE '%".$Ambulatory."%') AND(`function`  LIKE '%".$Function."%')"
) or die(mysql_error());

There is another criteria I want to add - Adult/Child. I have date of birth as a column in the DB. If the candidate is above 18, would fall under Adult, otherwise Child.

The user may want to search for an adult with all the contents in $Odata. How can I do this?

looking through Calculate Age in MySQL (InnoDb) and Search age range in mysql, php I understand it can be done independently, but how can I incorporate it into my above query. Is that possible?

Community
  • 1
  • 1
user2510479
  • 1,528
  • 13
  • 17
  • 1
    off your question topic, but you might like to consider changing to mysqli /PDO functions because the mysql_* ones are depreciated. Would also be worth reading about prepared statements and sql injections. – Ian Kenney Jul 02 '13 at 21:24
  • I'd like to find the function `foo%'; --` – Rob W Jul 02 '13 at 21:28
  • TIMESTAMPDIFF(YEAR,date_of_birth,CURDATE()) will give current age if `date_of_birth` is a `timestamp` field. –  Jul 02 '13 at 21:37

2 Answers2

0

This might help

sample data

create table dob
( 
    dob datetime
  ) 
;
insert into dob select '2001-10-08' ;
insert into dob select '1976-11-28' ;

Figure out child or adult:

select 
 dob, 
 case when dob  > Current_date() - INTERVAL 18 YEAR then 'child' else 'adult' end
from dob

see http://sqlfiddle.com/#!2/2df6d/12

Ian Kenney
  • 6,376
  • 1
  • 25
  • 44
  • 1
    I don't know how mySQL handles stuff like this, but you'd probably be better off using `dob > Current_date() - INTERVAL 18 YEAR`, as the optimizer then has a (more easily recognizable) chance at performing the date math _once_, as opposed to for every row. – Clockwork-Muse Jul 02 '13 at 22:32
  • Thanks @Ian Kenny, I think that is what I will do! – user2510479 Jul 03 '13 at 13:48
0

You can find a correct answer in the official MySQL documentation:

SELECT CASE WHEN TIMESTAMPDIFF(YEAR,dob,CURDATE()) >=18 THEN 'adult' ELSE 'child' END
FROM p_candidate;
Alejandro Colorado
  • 6,034
  • 2
  • 28
  • 39