Giving column priority first in SQL Search (LIKE) statement
This is my SQL Search statement.......
I want to return values on column NAME with first priority and then column Desc_Work.
Select * from posts
where Province = 'Western_Cape'
and 开发者_如何学CNAME LIKE '%Etienne%'
or Desc_Work LIKE '%Etienne%'
What changes must I make to this to make sure that the rows in column NAME will be displayed first?
ORDER BY CASE
WHEN NAME LIKE '%Etienne%' THEN 1
ELSE 2
END
I also believe that you wanted 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 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%')
...
You can implement Full Text Search and rank the results. I think another question asked something similar here: Giving precedence/weight to a column using FREETEXTTABLE
you can simply write this :
Select NAME, posts.* from posts where Province = 'Western_Cape' and NAME LIKE 'Etienne%' or Desc_Work LIKE '%Etienne%'
精彩评论