|
|||||||
New to SitePoint Forums? Register here for free!
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
SitePoint Zealot
![]() ![]() 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 ???
|
|
|
|
|
|
#2 |
|
SQL Consultant
![]() ![]() ![]() ![]() 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"
|
|
|
|
|
|
#3 |
|
SitePoint Zealot
![]() ![]() Join Date: Mar 2005
Location: Madison, WI
Posts: 165
|
Cool, thanks. MySQL is so cool.
|
|
|
|
|
|
#4 | |
|
SitePoint Zealot
![]() ![]() 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
Quote:
|
|
|
|
|
|
|
#5 |
|
reads the Community Crier
![]() ![]() ![]() 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.
|
|
|
|
|
|
#6 |
|
SitePoint Zealot
![]() ![]() Join Date: Mar 2005
Location: Madison, WI
Posts: 165
|
Thanks. It works now.
|
|
|
|
|
|
#7 |
|
SQL Consultant
![]() ![]() ![]() ![]() 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"
|
|
|
|
|
|
#8 |
|
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!
|
|
|
|
|
|
#9 |
|
SitePoint Zealot
![]() ![]() 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.. |
|
|
|
![]() |
| Bookmarks |
«
Previous Thread
|
Next Thread
»
| Thread Tools | |
| Display Modes | |
|
|
|
All times are GMT -7. The time now is 03:49.













Linear Mode
