Codingmysql search

 

Press Ctrl+Enter to quickly submit your post
Quick Reply  
 
 
  
 From:  Peter (BOUGHTONP)  
 To:  andy      
33373.2 In reply to 33373.1 
(col1 LIKE %col1search%) is a boolean, which should convert to 1/0

So you could do something along the lines of:

SELECT blah, int(col1 LIKE %col1search%)+int(col2 LIKE %col1search%) AS score
...


Which still doesn't seem ideal, and I don't know what it'll do performance-wise, but it should do what you want?
0/0
 Reply   Quote More 

 From:  andy   
 To:  Peter (BOUGHTONP)     
33373.3 In reply to 33373.2 
ha. i've done this based on that link i mentioned at the end of the first post:

php code:
function get_search_sql($select_columns, $table, $search_fields, $search_terms, $group_by="", $limit="")
{
	$search = array_combine($search_fields, $search_terms);
 
	$count = 0;
	foreach($search as $field => $term) {
		
		if (trim($term) == "")
			continue;
	
		if (!get_magic_quotes_gpc()) {
			$term = addslashes($term);
		}
		
		if (!$count) {
			$sql_fields = "";
			$sql_fields_replace = "";
			
		} else {
			$sql_fields.= ",";
			$sql_fields_replace.= ",";
			
		}
		
		$sql_fields.= $field;
		
		$terms = explode(" ", $term);
		
		$tmp_fields_replace = "LOWER($field)";
 
		foreach($terms as $term) {
			if ($count)
				$sql_search.= "OR ";
				
			$term = trim(strtolower($term));
				
			$tmp_fields_replace = "REPLACE($tmp_fields_replace,'$term','')";
			$sql_search.= "LOWER($field) LIKE '%$term%' ";
			
			$count++;
		}
		
		$sql_fields_replace.= $tmp_fields_replace;
	}
	
	if (trim($group_by) != "")
		$group_by = "GROUP BY $group_by";
	if (trim($limit) != "")
		$limit = "LIMIT $limit";
	
	return "SELECT $select_columns, ( LENGTH(CONCAT($sql_fields)) - LENGTH(CONCAT($sql_fields_replace)) ) / LENGTH(CONCAT($sql_fields)) AS relevancy FROM $table WHERE $sql_search $group_by ORDER BY relevancy DESC $limit";
}


which, when called with get_search_sql("id", "table", array("col1", "col2"), array("col1word1 col1word2", "col2word1", "")), results in SQL such like:

sql code:
SELECT id, ( LENGTH(CONCAT(col1,col2)) - LENGTH(CONCAT(REPLACE(REPLACE(LOWER(col1),'col1word1',''),'col1word2',''),REPLACE(LOWER(col2),'col2word1',''))) ) / LENGTH(CONCAT(col1,col2)) AS relevancy 
FROM TABLE 
WHERE LOWER(col1) LIKE '%col1word1%' OR LOWER(col1) LIKE '%col1word2%' OR LOWER(col2) LIKE '%col2word1%' 
ORDER BY relevancy DESC


beautiful, right? yours is good but i think the 'relevancy' score of my one should be a bit more accurate (it's based on the number of occurences rather than just a sum of true/falses). no idea about performance as my DB is too small to test it properly. i don't know if i care enough.
0/0
 Reply   Quote More 

 From:  DarkBadger  
 To:  andy      
33373.4 In reply to 33373.3 
That's what I would of done. Not Peter's.
0/0
 Reply   Quote More 

 From:  andy   
 To:  DarkBadger     
33373.5 In reply to 33373.4 
you don't even know.
0/0
 Reply   Quote More 

 From:  DarkBadger  
 To:  andy      
33373.6 In reply to 33373.5 
I do so know. I would of done yours and not Peters cause hes not retarded and types ok but what comes out is retarded and he really really don't even know.
0/0
 Reply   Quote More 

 From:  Serg (NUKKLEAR)  
 To:  DarkBadger     
33373.7 In reply to 33373.4 
quote:
would HAVE done


(are you Wattsy? :( )
[...Insert Brain Here...]
0/0
 Reply   Quote More 

 From:  DarkBadger  
 To:  Serg (NUKKLEAR)     
33373.8 In reply to 33373.7 

0/0
 Reply   Quote More 

 From:  andy   
 To:  DarkBadger     
33373.9 In reply to 33373.6 
you know so little!
0/0
 Reply   Quote More 

Reply to All    
 

1–9

Rate my interest:

Adjust text size : Smaller 10 Larger

Beehive Forum 1.5.2 |  FAQ |  Docs |  Support |  Donate! ©2002 - 2024 Project Beehive Forum

Forum Stats