Hi i am new to drupal 7 and also to sql query builder format.i have a requirement to search the string. in that i hav to display exact matched string at the top followed by related matches. i explode a string with space and searched a each word exist or not.the code as follows.
$keys -> contain the search term
$keys = preg_replace('!\*+!', '%', $keys);
$keys = explode(' ', trim(str_replace(array( '\'', '"', ',' , ';', '<', '>','!','_','-','+','@','#','$','%','^','&','.'), ' ', $keys)));;
$query = db_select('file')->extend('PagerDefault');
$query->fields('file', array('fid','filename'))
->fields('fs', array('e_id','f_subtitle_value'));
$subquery = db_select('field_data_sub','fs');
$subquery->Fields('fs', array('e_id','f_subtitle_value'));
$subquery->condition('fs.e_type', 'node','!=');
$query->leftjoin($subquery,'fs','file.fid = fs.e_id');
$db_or = db_or();
foreach ($keys as $key) {
if(!(empty($key))) {
$db_or->condition('filename', "%$key%", 'LIKE');
}
}
$query->condition($db_or);
$query->condition(db_and()->
condition('filemime', 'image/jpeg', '!=')->
condition('filemime', 'image/png', '!='));
i tried union all - wrote two query and combine it.. it fetches output but the exact searched result came twice . is that a gud way or else is there any other way.. please suggest the best way :)