I'm currently working on a databank project with mysql and php where one particular query is giving me an enormous headache.
In this project we have to search subcontractors for skills like java, c++ etc and evaluate if their skill rating is equal or grater to the rating value asked for.
We have a database scheme which looks something like that:
table subs:
table regions:
table skills:
table skillratings:
table subtoskill:
The subtoskill Table holds the N*N relations of subcontractors to skills, so an entry with "1, 2, 3, 4" would state that the subcontractor with id 2 has the skill with id 3 and the rating with id 4.
Now I want to query multiple skills with an AND or OR connection, for example "which subcontractors have skill java with value>=2 AND c++ with value>=4".
The OR query works perfectly(code below) but the AND query gives 0 results, except if the same skill with different values is picked.
/*
* selectSubsBySkill
* search database for subcontractors with given skills and ratings
* parameter:
* -$skill_ids array of ids(int) of skills, same size as $ratings
* -$ratings array of rating values(int) for skills, same size as $skill_ids
* -$and boolean true if and connection, else or connection of skills
* return:
* array with matching subcontractors information
*/
public function selectSubsBySkill($skill_ids, $ratings, $and){
if(empty($skill_ids)){
return $this->selectSubs();
}
$sel = 'SELECT * FROM '.SUBS.','.REGIONS.','.SKILLS.','.SUBTOSKILL.','.SKILLRATINGS.' WHERE '.
SUB_REGION_ID.'='.REGION_ID.' AND '.SUBTOSKILL_SUB_ID.'='.SUB_ID.' AND '.
SUBTOSKILL_SKILL_ID.'='.SKILL_ID.' AND '.SKILLRATING_ID.'='.SUBTOSKILL_RATING_ID.' AND (';
$i=0;
foreach($skill_ids as $skill_id){
$sel .= '('.SKILLRATING_VALUE.'>='.$ratings[$i].' AND '.SUBTOSKILL_SKILL_ID.'='.$skill_id.')';
if($i<sizeof($skill_ids)-1){
if($and)$sel .= ' AND ';
else $sel .= ' OR ';
}
$i++;
}
$sel .= ') GROUP BY '.SUB_ID;
return $this->query($sel);
}
I've tried several other examples including queries with UNIONs or JOINs but still nothing has worked.
I'd be glad if someone here got a hint to solve this problem.