Click here to Skip to main content
15,898,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to make a search box in my site to search the movie titles from the mysql database. I am facing a problem in sorting the search results.
Let me tell you the problem.
I get the search string entered by the user, then I split this string into words and store these words into an array(str[]). Now I am using this query to search from the database.
suppose str array has five words.

query = "select from tbl_name where moviename = str[0] ";
query .= "or moviename = str[1] or moviename = str[2] ";
query .= "or moviename = str[3] or moviename = str[4]";




As we know this query will retrieve all the movie titles from the database which contains any of the word of the array.

My Problem is, I want to keep those movie titles on the top which has more keywords of the array (str).
Suppose If a movie title contains all (5) the words of the array (str) then It should be on the top. If a movie title contains (4) words of the array (str) it should on the second number and so on.
How can I do that?
Posted

1 solution

Here is an idea. Do not know if it works for you.

You can do a specified ORDER BY.
SQL
SELECT FROM tbl_name WHERE moviename = str[0] OR moviename = str[1] ORDER BY moviename = str[0], moviename = str[1]
 
Share this answer
 
v5
Comments
Kim Togo 28-May-11 8:32am    
Perhaps - http://www.codediesel.com/mysql/sorting-mysql-rows-using-column-values/

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900