Counting matches

This is a discussion on Counting matches within the PHP Language forums, part of the PHP Programming Forums category; Hi, This is really more of a MySQL question than PHP per se, but the only MySQL group my newserver ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-18-2005
Chris
 
Posts: n/a
Default Counting matches

Hi,

This is really more of a MySQL question than PHP per se, but the only
MySQL group my newserver carries seems to be pretty quiet, so I
thought I'd ask here.

What I am trying to do is determine the number of terms which match a
field in the database where an initial string of multiple words has
been broken up into single terms and the SQL statement is built by
looping through each term separately, something like this:

$original_string = "this is a test string"

// code to remove stop words/common terms leaves

$trimmed = "this test string"

// this is then exploded at the space into an array of its constituent
words. This array is then looped over to pass each word to an SQL
query thus

$SQL = "select * from main where"

while(there are terms){

$SQL .= "Title like '%" . $term . "%' OR"

}

// this would lead, using the above example, to an SQL query which
looks like this:

$SQL = "SELECT * FROM main WHERE Title like '%this%' OR Title like
'%test%' OR Title like '%string%'

What I need to do is to check how many of the terms actually match
something. Is there any way, either in MySQL or with the result array
in PHP, to determine this?

Am I on completely the wrong course (as it seems to me at the
moment...)

tia

Chris

Reply With Quote
  #2 (permalink)  
Old 05-18-2005
Ewoud Dronkert
 
Posts: n/a
Default Re: Counting matches

On Wed, 18 May 2005 10:29:15 +0100, Chris wrote:
> What I need to do is to check how many of the terms actually match
> something.


I think the best way might be to maintain a separate index table with
terms. New search terms will be a bit slower. Alternatively, you could
just, in addition to your original query, have separate queries for each
term (using count() or using the rowcount of each result).


--
Firefox Web Browser - Rediscover the web - http://getffox.com/
Thunderbird E-mail and Newsgroups - http://gettbird.com/
Reply With Quote
  #3 (permalink)  
Old 05-18-2005
Chris
 
Posts: n/a
Default Re: Counting matches

On Wed, 18 May 2005 11:49:14 +0200, Ewoud Dronkert
<firstname@lastname.net.invalid> wrote:

>have separate queries for each
>term (using count() or using the rowcount of each result).



Hmmmm, that might just work, thanks! I think I've been staring at this
problem for so long that I missed the obvious!

Thanks again

C

Reply With Quote
  #4 (permalink)  
Old 05-18-2005
Daniel Tryba
 
Posts: n/a
Default Re: Counting matches

Chris <Chris.Stephens@nez.oc.ku> wrote:
[snip]
> What I need to do is to check how many of the terms actually match
> something. Is there any way, either in MySQL or with the result array
> in PHP, to determine this?
>
> Am I on completely the wrong course (as it seems to me at the
> moment...)


Ehhhh, it looks to me like you are trying to reinvent full text
searches (http://dev.mysql.com/doc/mysql/en/fulltext-search.html)

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 11:51 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0