SQL question. how relevent are the search results ?
| From: | Chris Lee | Date: | Fri, 16 Nov 2001 20:03:55 +0000 |
| Subject: | SQL question. how relevent are the search results ? | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-74926@lists.php.net to get a copy of this message | ||
if I give the user a search and he enters 'cat dog' I am going to want to
find all the results that have the word 'cat' or 'dog' in them, but I want
all the results that have 'cat' and 'dog' at the beginning of the results
because these would be more relevent. now how I see it is this, it aint
pretty.
$result = select * from product where product_feature like '%cat%' and
product_feature like '%dog%'
foreach( $result as $pos => $val )
$name[$val['product_id']] = $val['product_name']
$result = select * from product where product_feature like '%cat%' or
product_feature like '%dog%'
foreach( $result as $pos => $val )
$name[$val['product_id']] = $val['product_name']
foreach( $name as $pos => $val )
echo "$val <br>";
this will put the 'AND' before the 'OR'. but its not nice, would be nice if
I could do this with one simple query. this is only with two keywords. with
three its gets exponentially nasty.
'cat dog mouse'
$result = select * from product where product_feature like '%cat%' and
product_feature like '%dog%' and product_feature like '%mouse%'
foreach( $result as $pos => $val )
$name[$val['product_id']] = $val['product_name']
$result = select * from product where product_feature like '%cat%' and
product_feature like '%dog%'
foreach( $result as $pos => $val )
$name[$val['product_id']] = $val['product_name']
$result = select * from product where product_feature like '%cat%' and
product_feature like '%mouse%'
foreach( $result as $pos => $val )
$name[$val['product_id']] = $val['product_name']
$result = select * from product where product_feature like '%dog%' and
product_feature like '%mouse%'
foreach( $result as $pos => $val )
$name[$val['product_id']] = $val['product_name']
$result = select * from product where product_feature like '%cat%' or
product_feature like '%dog%' or product_feature like '%mouse%'
foreach( $result as $pos => $val )
$name[$val['product_id']] = $val['product_name']
foreach( $name as $pos => $val )
echo "$val <br>";
ouch, this is just nasty, not good. there must be a better way. accually
writing code to take a dynamic number of keywords is guna be ugly.
any SQL idea's ? yes I know this is a php forum, but its related.
--
Chris Lee
lee@mediawaveonline.com