Solved

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

Posted on 2008-06-17
8
294 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
 
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
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Loading csv or delimited data files to MySQL database is a very common task frequently questioned about and almost every time LOAD DATA INFILE comes to the rescue. Here we will try to understand some of the very common scenarios for loading data …
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 …
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

759 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now