Link to home
Start Free TrialLog in
Avatar of mindwarpltd
mindwarpltd

asked on

Need a query to match the most popular records based on a list of matches?

I have the following table.

CREATE TABLE `keywords` (
  `Word` varchar(20) NOT NULL,
  `PadID` bigint(20) NOT NULL,
  `LetterIdx` varchar(1) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

And heres some example data.

INSERT INTO `keywords` (`Word`, `PadID`, `LetterIdx`) VALUES
('error', 65010, 'E'),
('fix', 65010, 'F'),
('clean', 65010, 'C'),
('windows cleaner', 65010, 'W'),
('registry tools', 65010, 'R'),
('windows tweak', 65010, 'W'),
('windows error', 65010, 'W'),
('system maintainance', 65010, 'S'),
('registry repair', 65010, 'R'),
('registry cleaner', 65010, 'R');

PadID is the key to my main table (My main table is called Pads, records for programs I show on my website, its a shareware download site), LetterIdx is the first letter of Word.
So I can product a list of word with the same letter.

Heres an example.
http://www.softtester.com/keywords-b.shtml

I have program page on my website, which show details about one program.
I need to create a list of 10 programs which match the current program based on keywords.

So ideally, I want a query which will find the most matches of keywords and order by the most number of matches.

PLEASE FEEL FREE TO ASK ANY QUESTIONS.
Avatar of Nellios
Nellios
Flag of Greece image

More or less what you need is the snippet bellow.
What it does is selects all rows that corresponds to the current product say 65010.
Then it joins all the keywords for the current product with all the keywords in your table.
It uses a group by products so that it can count matching words per product.
It sorts your data that way and limits the query to the first 10 results.

Hope that helps
select count(match_keywords.Word) as matching_words ,match_keywords.PadID
from keywords current_program_keywords
left join keywords match_keywords on match_keywords.Word=current_program_keywords.Word
where match_keywords.Word IS NOT NULL
and current_program_keywords.PadID=65010
group by match_keywords.PadID
order by matching_words DESC
LIMIT 0,10

Open in new window

Avatar of mindwarpltd
mindwarpltd

ASKER

Ah great :)

Don't suppose you can also join into the pads table so I can use one query to get all the data?
The key is PadID
Unless you think the query will be to intensive.
If so can you provide another query to get those Pad Records
The query I posted above selects the number of matches and the PadID.
If I get it right it already does what you want.
Yeah, I'd like you to add pads.padid = yourquery.paid  and select pads.*
ASKER CERTIFIED SOLUTION
Avatar of Nellios
Nellios
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Right...
That query took 10 seconds, the first query took 0.002 seconds.

I was going to ask you if I should have an index on the word field.

Or do you think I should use something like AND PadID IN ($list_of_ids) ? with an extra query ?
An index on Work will speed things up.