Return best matched search results?


I am querying an Access DB based on a multiple field search, some of which using the LIKE '%%'  function and i would like to know if there's a way of returning the results ORDERED by the best matches first... the search itself is optional conditional which is why i'm having trouble with the result set.


Table :


SELECT * FROM Table WHERE (RequestID>=1 AND RequestID<=20000) OR SubmitterName LIKE '%MaxOvrdrv%' OR DirectorName LIKE '%MyDirector%' OR AssignedTo='MaxOvrdrv'

That query will return me 20 000 results or more because of the requestID query, but i would like to have the results that are within 20 000, and match the most of the other fields queried first... example, i would like to have:

20 222 - MaxOvrdrv - MyDirector - MaxOvrdrv

result come up FIRST instead of

1 - jamie - julie - jamie

which only matches 1 search criteria instead of 4 or 3 or 2...

So is there a way to do that directly into the SQL Query??

thank you!
LVL 10
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

you can create a formula that weights it and order by that.
FROM Table 
WHERE (RequestID>=1 AND RequestID<=20000) 
OR SubmitterName LIKE '%MaxOvrdrv%' 
OR DirectorName LIKE '%MyDirector%' 
OR AssignedTo='MaxOvrdrv'
ORDER BY iif((RequestID>=1 AND RequestID<=20000),1,0)+iif(SubmitterName LIKE '%MaxOvrdrv%',1,0)+iif(DirectorName LIKE '%MyDirector%',1,0)+iif(AssignedTo='MaxOvrdrv',1,0) DESC

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
you can also weight values by making their value larger than 1, thus more important.
MaxOvrdrv2Author Commented:
ok, sounds good...

i just tried to used that solution with the data above but selected more parameters than described in it, and i got a "No value given" error. Does that mean that i must use all the parameters used in the query itself in the ORDER BY calculation?

MaxOvrdrv2Author Commented:
Worked! Just had to make sure that all the fields referenced in the query were also weighed in the order by... thanks a lot!!! =)
MaxOvrdrv2Author Commented:
Great job... exactly what i wanted! and on top of that, you just gave me an idea to make the user search even more friendly by letting them decide which field will have more importance in their search! =)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.