I need to implement a simple search in a table of small content: id, name, description, content. Results should be ranked by priority.
this means that if the search word was found in the description field, it will be displayed only after all the lines that have a dog in the name field
What I've done:
I tried to create a temporary table with a structure similar to the table I use, but with a different field priority. for each field that I use to search, insert select into temp table
Example:
DECLARE @query NVARCHAR(255)
CREATE TABLE
(
[id] INT NOT NULL ,
[name] NVARCHAR(255) ,
[description] NVARCHAR(MAX) ,
[content] NVARCHAR(MAX) ,
[priority] INT
)
INSERT INTO
( [ID] ,
[name] ,
[description] ,
[content] ,
[priority]
)
SELECT [ID] ,
[name] ,
[description] ,
[content] ,
1
FROM [tbl_content]
WHERE name LIKE '%' + @query + '%'
INSERT INTO
( [ID] ,
[name] ,
[description] ,
[content] ,
[priority]
)
SELECT [ID] ,
[name] ,
[description] ,
[content] ,
2
FROM [tbl_content]
WHERE description LIKE '%' + @query + '%'
AND id NOT IN ( SELECT id
FROM
SELECT *
FROM
ORDER BY [priority]
DROP TABLE
Sasha source
share