Giving column priority first in SQL Search (LIKE) statement

This is my SQL Search query .......

I want to return the values ​​in the NAME column with the first priority, and then in the Desc_Work column.

Select * from posts where Province = 'Western_Cape' and NAME LIKE '%Etienne%' or Desc_Work LIKE '%Etienne%' 

What changes should I make to ensure that the rows in the NAME column are displayed first?

+4
source share
3 answers
 ORDER BY CASE WHEN NAME LIKE '%Etienne%' THEN 1 ELSE 2 END 

I also think you like where Province = 'Western_Cape' and (NAME LIKE '%Etienne%' or Desc_Work LIKE '%Etienne%')

UPDATE

 Select * from posts where Province = 'Western_Cape' and (NAME LIKE '%Etienne%' or Desc_Work LIKE '%Etienne%') ORDER BY CASE WHEN NAME LIKE '%Etienne%' THEN 1 ELSE 2 END 

I assume that you need (NAME LIKE '%Etienne%' or Desc_Work LIKE '%Etienne%') in brackets - your original query returns records where (Province = 'Western_Cape' and NAME LIKE '%Etienne%' ) or Desc_Work LIKE '%Etienne%') ...

+11
source

You can implement full-text search and rank the results. I think another question asked something similar here: Giving priority / weight to a column using FREETEXTTABLE

0
source

you can simply write this:

Select NAME, posts.* from posts where Province = 'Western_Cape' and NAME LIKE 'Etienne%' or Desc_Work LIKE '%Etienne%'

0
source

All Articles