Solved

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

Posted on 2008-06-17
8
295 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
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

This guide whil teach how to setup live replication (database mirroring) on 2 servers for backup or other purposes. In our example situation we have this network schema (see atachment). We need to replicate EVERY executed SQL query on server 1 to…
I have been using r1soft Continuous Data Protection (http://www.r1soft.com/linux-cdp/) for many years now with the mySQL Addon and wanted to share a trick I have used several times. For those of us that don't have the luxury of using all transact…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

914 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

18 Experts available now in Live!

Get 1:1 Help Now