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 > how to sort by most keyword found first

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-13-08, 19:04
gilgalbiblewhee gilgalbiblewhee is offline
Registered User
 
Join Date: Jul 2004
Posts: 494
how to sort by most keyword found first

How do I sort something like this by most keyword found first?
Code:
SELECT * 
  FROM bible 
 WHERE 1=1 
   AND CASE WHEN text_data LIKE '%there%' 
            THEN 1 ELSE 0 END
     + CASE WHEN text_data LIKE '%house%' 
            THEN 1 ELSE 0 END
     + CASE WHEN text_data LIKE '%daughter%' 
            THEN 1 ELSE 0 END
      > 4
I read this:
http://r937.com/keyword_relevance.html
But I don't think any of the cases mentioned applies to what I'm looking for.
__________________
Compare bible texts (and other tools):
TheWheelofGod
Reply With Quote
  #2 (permalink)  
Old 08-13-08, 19:15
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
in order to sort by a column, you should put that column into the SELECT clause
Code:
SELECT *
  FROM (
       SELECT bible.* 
            , CASE WHEN text_data LIKE '%there%' 
                   THEN 1 ELSE 0 END
            + CASE WHEN text_data LIKE '%house%' 
                   THEN 1 ELSE 0 END
            + CASE WHEN text_data LIKE '%daughter%' 
                   THEN 1 ELSE 0 END
              AS relevance      
         FROM bible
        WHERE text_data LIKE '%there%'
           OR text_data LIKE '%house%'
           OR text_data LIKE '%daughter%'
      ) AS d 
 WHERE relevance > 4
ORDER
    BY relevance
by the way, it's going to be pretty difficult to reach a score greater than 4 if you're only scoring up to 3 keywords
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 08-13-08, 21:00
gilgalbiblewhee gilgalbiblewhee is offline
Registered User
 
Join Date: Jul 2004
Posts: 494
Quote:
Originally Posted by r937
in order to sort by a column, you should put that column into the SELECT clause
Code:
SELECT *
  FROM (
       SELECT bible.* 
            , CASE WHEN text_data LIKE '%there%' 
                   THEN 1 ELSE 0 END
            + CASE WHEN text_data LIKE '%house%' 
                   THEN 1 ELSE 0 END
            + CASE WHEN text_data LIKE '%daughter%' 
                   THEN 1 ELSE 0 END
              AS relevance      
         FROM bible
        WHERE text_data LIKE '%there%'
           OR text_data LIKE '%house%'
           OR text_data LIKE '%daughter%'
      ) AS d 
 WHERE relevance > 4
ORDER
    BY relevance
by the way, it's going to be pretty difficult to reach a score greater than 4 if you're only scoring up to 3 keywords:)
Oh yeah. That was taken from another thread of mine. The actual sequel has more words.
I don't understand the meaning of this:
Code:
AS d
__________________
Compare bible texts (and other tools):
TheWheelofGod
Reply With Quote
  #4 (permalink)  
Old 08-13-08, 21:20
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
it assigns the alias "d" to the derived table i.e. subquery
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 08-13-08, 21:53
gilgalbiblewhee gilgalbiblewhee is offline
Registered User
 
Join Date: Jul 2004
Posts: 494
Hmmm...so I have to repeat?
I guess the first time around is to find the keywords and the 2nd is to sort.
Code:
            , CASE WHEN text_data LIKE '%there%' 
                   THEN 1 ELSE 0 END
            + CASE WHEN text_data LIKE '%house%' 
                   THEN 1 ELSE 0 END
            + CASE WHEN text_data LIKE '%daughter%' 
                   THEN 1 ELSE 0 END
              AS relevance      
         FROM bible
        WHERE text_data LIKE '%there%'
           OR text_data LIKE '%house%'
           OR text_data LIKE '%daughter%'
And what does relevance do?
__________________
Compare bible texts (and other tools):
TheWheelofGod
Reply With Quote
  #6 (permalink)  
Old 08-13-08, 22:01
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
relevance is the name assigned to the column which contains the result of the calculation

i added the WHERE clause simply to make your query more efficient -- no sense including, or calculating the relevance for, a row that has none of the keywords at all
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

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