Return best matched search results?

Hi,

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.

example:

Table :
RequestID
SubmitterName
DirectorName
AssignedTo

QUERY:

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
MaxOvrdrv2Asked:
Who is Participating?
 
mfsamuelConnect With a Mentor Commented:
you can create a formula that weights it and order by that.
SELECT * 
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

0
 
mfsamuelCommented:
you can also weight values by making their value larger than 1, thus more important.
0
 
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?

Thanks!
0
 
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!!! =)
0
 
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! =)
0
All Courses

From novice to tech pro — start learning today.