Solved

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

Posted on 2008-06-17
8
301 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
Creating and Managing Databases with phpMyAdmin in cPanel.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

728 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