Search query, priority "order by"

I need to implement a simple search in a table of small content: id, name, description, content. Results should be ranked by priority.

  • name
  • Description
  • Content

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 #tbl_search
    (
      [id] INT NOT NULL ,
      [name] NVARCHAR(255) ,
      [description] NVARCHAR(MAX) ,
      [content] NVARCHAR(MAX) ,
      [priority] INT
    )    


    --searching in name field
  INSERT    INTO #tbl_search
            ( [ID] ,
              [name] ,
              [description] ,
              [content] ,
              [priority]

            )
            SELECT  [ID] ,
                    [name] ,
                    [description] ,
                    [content] ,
                    1
            FROM    [tbl_content]
            WHERE   name LIKE '%' + @query + '%'

    --searching in description field            
  INSERT    INTO #tbl_search
            ( [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    #tbl_search )
   --.....           

  SELECT    *
  FROM      #tbl_search
  ORDER BY  [priority]

  DROP TABLE #tbl_search
+4
source share
3 answers

- CASE :

SELECT name, description, content,
    priority = CASE
        WHEN name LIKE '%' + @query + '%' THEN 1
        WHEN desription LIKE '%' + @query + '%'  THEN 2
        WHEN content LIKE '%' + @query + '%'  THEN 3
    END CASE
FROM tbl_content
WHERE
    name LIKE '%' + @query + '%' OR
    desription LIKE '%' + @query + '%'  OR
    content LIKE '%' + @query + '%'
ORDER BY priority ASC
+5

- UNION:

SELECT name AS text FROM tbl_content WHERE name LIKE '%' + @query + '%'
UNION
SELECT description AS text FROM tbl_content WHERE desription LIKE '%' + @query + '%'
UNION
SELECT content AS text FROM tbl_content WHERE content LIKE '%' + @query + '%'
+1

To complete the picture, I moved the brilliant @Amirshk solution to the next level, using the case where the order clause (mysql query):

SELECT name, description, content
FROM tbl_content
WHERE
    name LIKE '%' + @query + '%' OR
    desription LIKE '%' + @query + '%'  OR
    content LIKE '%' + @query + '%'
ORDER BY CASE
        WHEN name LIKE '%' + @query + '%' THEN 1
        WHEN desription LIKE '%' + @query + '%'  THEN 2
        WHEN content LIKE '%' + @query + '%'  THEN 3
    END
0
source

Source: https://habr.com/ru/post/1732733/


All Articles