You should use full text search CONTAINSTABLE
to find the top 100 (possibly 200) candidate results and then order the results you found using your own criteria.
It sounds like you'd like to ORDER BY
- exact match of the phrase (
=
) - the fully matched phrase (
LIKE
) - higher value for the
Popularity
column - the
Rank
from theCONTAINSTABLE
But you can toy around with the exact order you prefer.
In SQL that looks something like:
DECLARE @title varchar(255)
SET @title = '"Toy Story"'
--need to remove quotes from parameter for LIKE search
DECLARE @title2 varchar(255)
SET @title2 = REPLACE(@title, '"', '')
SELECT
m.ID,
m.title,
m.Popularity,
k.Rank
FROM Movies m
INNER JOIN CONTAINSTABLE(Movies, title, @title, 100) as [k]
ON m.ID = k.[Key]
ORDER BY
CASE WHEN m.title = @title2 THEN 0 ELSE 1 END,
CASE WHEN m.title LIKE @title2 THEN 0 ELSE 1 END,
m.popularity desc,
k.rank
See SQLFiddle