EDIT: As can be seen, I decided to go with mySQL’s “Match”. That said, if someone knows of a clean method to do what I wanted within a SELECT statement, I would appreciate the information (knowledge for knowledge sake and all that)
I’m currently working on developing a local search engine for a website I’m designing, and as such one way in which I am using to determine the relevance of articles is the number of times the search terms appear in the article itself. As such, I’m looking for an SQL query that will allow me to pull rows (articles) containing the search term, and than order them based on how many times the search terms appear in each row (articles).
In other words, I need something like this…
SELECT article_id FROM articles_table WHERE article_content LIKE '%Search Terms%' ORDER BY COUNT(number of times string appears in article_content);
So if a user were to search for “The Empire” and pulled up the following three articles…
- The Empire is The Empire.
- The Empire is the name of a position in baseball.
- The Empire The Empire The Empire.
It would sort them as so..
- The Empire The Empire The Empire
- The Empire is The Empire
- The Empire is the name of a position in baseball.
I am working in PHP, and although ideally I would like to perform this operation with nothing more then one SQL query, I’m open to PHP solutions if this is not possible.
Any and all help is greatly appreciated.
You should really consider a Full Text search solution. Either use MyISAM tables and MySQL native full text search, or you can go the external way and use something like Sphinx fulltext search or Lucene