Go Back   SitePoint Forums > Forum Index > Program Your Site > Databases > MySQL
Newsletter FAQ Members List Calendar Mark Forums Read

New to SitePoint Forums? Register here for free!

SitePoint Sponsor
 
Reply
 
Thread Tools Display Modes
Old Apr 5, 2006, 17:13   #1
soapergem
SitePoint Zealot
 
soapergem's Avatar
 
Join Date: Mar 2005
Location: Madison, WI
Posts: 165
MySQL - order by relevance

I'm fairly new to SQL, and I'm wondering if there's any built-in way of ordering results by how well they matched a given query. For instance, let's say we have this query:
Code:
SELECT *
FROM `items`
WHERE `description` LIKE 'quick'
AND (
     `description` LIKE 'brown'
  OR `description` LIKE 'fox'
  OR `description` LIKE 'lazy'
  OR `description` LIKE 'dog'
)
LIMIT 0 , 30
ORDER BY ???
Then, if I had the following rows of data...
  1. The quick brown dog jumped fox-like.
  2. The quick brown fox jumped over the lazy dog.
  3. The quick fox jumped over the Sears Tower.
...I would want them to be ordered {2, 1, 3} because that's pretty obviously the order of what best matches the query. I have no idea if this possible or not, but I have learned that often times other people have already thought of the cool features I'd like so and accordingly I find they're already implemented. So I'm crossing my fingers. Thanks!
soapergem is offline   Reply With Quote
Old Apr 5, 2006, 18:41   #2
r937
SQL Consultant
silver trophybronze trophy
SitePoint Award Recipient
 
r937's Avatar
 
Join Date: Jul 2002
Location: Toronto, Canada
Posts: 32,861
ORDER
BY case when `description` LIKE 'brown' then 1 else 0 end
+ case when `description` LIKE 'fox' then 1 else 0 end
+ case when `description` LIKE 'lazy' then 1 else 0 end
+ case when `description` LIKE 'dog' then 1 else 0 end
DESC
__________________
r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
"giving out my real stuffs"
r937 is online now   Reply With Quote
Old Apr 5, 2006, 19:05   #3
soapergem
SitePoint Zealot
 
soapergem's Avatar
 
Join Date: Mar 2005
Location: Madison, WI
Posts: 165
Cool, thanks. MySQL is so cool.
soapergem is offline   Reply With Quote
Old Apr 5, 2006, 19:21   #4
soapergem
SitePoint Zealot
 
soapergem's Avatar
 
Join Date: Mar 2005
Location: Madison, WI
Posts: 165
So, I tried this, as suggested:
Code:
SELECT *
FROM `items`
WHERE `description` LIKE 'quick'
AND (
  `description` LIKE 'brown'
  OR `description` LIKE 'fox'
  OR `description` LIKE 'lazy'
  OR `description` LIKE 'dog'
)
LIMIT 0 , 30
ORDER BY (
  (
    CASE WHEN `description` LIKE 'brown'
    THEN 1
    ELSE 0
    END
  ) + (
    CASE WHEN `description` LIKE 'fox'
    THEN 1
    ELSE 0
    END
  ) + (
    CASE WHEN `description` LIKE 'lazy'
    THEN 1
    ELSE 0
    END
  ) + (
    CASE WHEN `description` LIKE 'dog'
    THEN 1
    ELSE 0
    END
  )
) DESC
LIMIT 0, 30
...and wound up with this:
Quote:
Originally Posted by phpMyAdmin
#1064 - You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'ORDER BY ((CASE WHEN `description` LIKE 'brown'
THEN 1
ELSE 0
EN
I got the same error with or without the parentheses, and I think I prefer them there for clarity, so that's why they're there. Thanks again.
soapergem is offline   Reply With Quote
Old Apr 5, 2006, 19:37   #5
longneck
reads the Community Crier
silver trophybronze trophy
SitePoint Award Recipient
 
longneck's Avatar
 
Join Date: Feb 2004
Location: Tampa, FL (US)
Posts: 9,894
you have limit in there twice. the limit clase comes after order by, so get rid of the first one.
longneck is offline   Reply With Quote
Old Apr 5, 2006, 19:42   #6
soapergem
SitePoint Zealot
 
soapergem's Avatar
 
Join Date: Mar 2005
Location: Madison, WI
Posts: 165
Thanks. It works now.
soapergem is offline   Reply With Quote
Old Apr 5, 2006, 19:49   #7
r937
SQL Consultant
silver trophybronze trophy
SitePoint Award Recipient
 
r937's Avatar
 
Join Date: Jul 2002
Location: Toronto, Canada
Posts: 32,861
you may also want to conside using wildcard characters in your LIKE strings, because otherwise you are going to return precious little (i.e. only those specific 1-word descriptions)
__________________
r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
"giving out my real stuffs"
r937 is online now   Reply With Quote
Old Apr 5, 2006, 20:17   #8
binjured
SitePoint Zealot
 
Join Date: Feb 2004
Location: Boston, MA
Posts: 188
Wow, I feel kind of dumb saying, but as a PHP/MySQL developer I didn't even realize this was possible! Not to derail this thread or anything (well, it is kinda concluded anyway), but could any of you SQL gurus suggest places to read more about "advanced" queries / techniques like this that don't involve just reading through the entire MySQL manual? Thanks!
binjured is offline   Reply With Quote
Old Apr 5, 2006, 21:20   #9
soapergem
SitePoint Zealot
 
soapergem's Avatar
 
Join Date: Mar 2005
Location: Madison, WI
Posts: 165
As far as using wildcard characters, r937, I had no intention of doing so. I was just using that for the example.

I had posted another question here, but I just figured it out on my own. I wanted to select this relevance value, and this is how it's done:
Code:
SELECT *, ((CASE WHEN `description` LIKE '%brown%' THEN 1 ELSE 0 END) + (CASE WHEN `description` LIKE '%fox%' THEN 1 ELSE 0 END) + (CASE WHEN `description` LIKE '%lazy%' THEN 1 ELSE 0 END) + (CASE WHEN `description` LIKE '%dog%' THEN 1 ELSE 0 END)) AS relevance
FROM `items`
WHERE `description`LIKE '%quick%' AND (
  `description`LIKE '%brown%'
  OR `description` LIKE '%fox%'
  OR `description` LIKE '%lazy%'
  OR `description` LIKE '%dog%'
)
ORDER BY relevance DESC
LIMIT 0 , 30;

Last edited by soapergem; Apr 6, 2006 at 02:07..
soapergem is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread | Next Thread »

Thread Tools
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 On
HTML code is Off

 
Forum Jump


All times are GMT -7. The time now is 03:49.


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Copyright 1998-2009, SitePoint Pty Ltd. All Rights Reserved