If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > SQL query help

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-07-11, 16:21
outofregs outofregs is offline
Registered User
 
Join Date: Apr 2011
Posts: 1
Question SQL query help

Hello,

I have 2 Tables (keywords & archive). The archive table is my main table and stores data for submitted pictures. The keywords table stores all keywords used in my site with 3 columns (id, keyword & numclicks). Currently I have a section on my site (OutOfRegs - Your source for military humor!) that lists the top 20 keywords and orders them by the 'numclicks' column. There is a column in the archives table titled 'keywords' and contains a string of keywords separated by a comma (ex: military, army, fail). I want to perform a query that groups keywords by the number of times they show up in 'archive.keywords', and then order the results by 'keywords.numclicks'. This query would be better than the previous because it would order keywords by their frequency on the site aas well as their number of clicks. Any ideas?
Reply With Quote
  #2 (permalink)  
Old 04-07-11, 22:22
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
comma-delimited values inside a single column violates first normal form, and the penalty you pay is complex queries and really terrible performance
Code:
SELECT k.keyword
     , COUNT(a.keyword) AS times_in_archive
  FROM keywords AS k
LEFT OUTER
  JOIN archive AS a
    ON FIND_IN_SET(k.keyword,a.keyword) > 0
GROUP
    BY k.keyword
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Tags
count, keywords, order, sql

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On