Solved

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

Posted on 2008-06-17
8
299 Views
Last Modified: 2008-06-17
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.
0
Comment
Question by:mindwarpltd
  • 4
  • 4
8 Comments
 
LVL 10

Expert Comment

by:Nellios
ID: 21800592
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

0
 

Author Comment

by:mindwarpltd
ID: 21800777
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
0
 

Author Comment

by:mindwarpltd
ID: 21800827
Unless you think the query will be to intensive.
If so can you provide another query to get those Pad Records
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 10

Expert Comment

by:Nellios
ID: 21800851
The query I posted above selects the number of matches and the PadID.
If I get it right it already does what you want.
0
 

Author Comment

by:mindwarpltd
ID: 21801014
Yeah, I'd like you to add pads.padid = yourquery.paid  and select pads.*
0
 
LVL 10

Accepted Solution

by:
Nellios earned 500 total points
ID: 21801048
Ok, all we have to do is to join the pads table, like the snipet below:
select count(match_keywords.Word) as matching_words ,pads.*
from keywords current_program_keywords
left join keywords match_keywords on match_keywords.Word=current_program_keywords.Word
inner join pads on pads.padID=match_keywords.PadID
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

0
 

Author Comment

by:mindwarpltd
ID: 21801100
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 ?
0
 
LVL 10

Expert Comment

by:Nellios
ID: 21801261
An index on Work will speed things up.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
xampp tool 12 58
Sql query on a varchar that is numeric. 8 47
java mysql insert application 14 41
reverse engineer .sql from php files 11 49
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question