Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search query, 'order by' priority

I need implement simple search in small content table: id, name, description, content. Results must be order by priorities

  1. name
  2. description
  3. content

it's mean that if searched word was found in description field, it will be shown only after all rows that have dog in name field

What I did:

I tried create temp table with structure like table that I use but with another field priority. for each field, these I use for search, do insert select to 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
like image 267
Sasha Avatar asked Feb 14 '10 11:02

Sasha


People also ask

How do you write a query for ORDER BY?

Syntax. SELECT column-list FROM table_name [WHERE condition] [ORDER BY column1, column2, .. columnN] [ASC | DESC]; You can use more than one column in the ORDER BY clause.

Does order of query matter?

No, the order of query parameters should not matter.

How do I set priority in mysql query?

You can use the LOW_PRIORITY or HIGH_PRIORITY in your queries depending on the type of query you execute: INSERT [LOW_PRIORITY | HIGH_PRIORITY] INTO ... SELECT [HIGH_PRIORITY] * FROM ... UPDATE [LOW_PRIORITY] table ...

Is ORDER BY before SELECT?

ORDER BY is evaluated before the SELECT, as the ordering changes the results returned. TOP 1 also ensures the lowest REVISION_NO is returned, therefore it appears you are using the query correctly. Save this answer.


2 Answers

One way of doing it would be using the CASE keyword:

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
like image 179
Amirshk Avatar answered Nov 15 '22 04:11

Amirshk


The easiest way would be to use UNION query:

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 + '%'
like image 37
Alex Shirshov Avatar answered Nov 15 '22 05:11

Alex Shirshov