Solved

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

Posted on 2008-06-17
8
296 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MySql hide the stored procedures 2 48
category table 2 24
change database name 2 28
BACKUP of mysql database from mysql server - using Coldfusion 9 36
Introduction In this article, I will by showing a nice little trick for MySQL similar to that of my previous EE Article for SQLite (http://www.sqlite.org/), A SQLite Tidbit: Quick Numbers Table Generation (http://www.experts-exchange.com/A_3570.htm…
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 Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

786 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