-1

If I have a table like

column
---------------------
his name is yyy   
What is your name  
my name is xxx 

If I had to search for words containing "What" & "Name" while using "like" and "OR" function, I would get the results but no order. How do I make the result containing both the words come first, and then any order

4 Answers4

0

From what I can understand about your question, you want the results ordered by alphabetical order. You can order results easily with the ORDER BY clause, such as SELECT * FROM tblExample ORDER BY name.

Unless you meant to ask something else of course, in which case I'd ask you to clarify your question so I can update my answer accordingly.

Chavez
  • 181
  • 11
0

You can try something like: SELECT * FROM table Where... ORDER BY FIELD(like ('[what][name]') and like )
DESC, name, alias_column;

didando8a
  • 130
  • 1
  • 7
0

With your are using text for search its convenient to use MATCH AGANIST (you must set the colum with FULLTEXT index)

Then you can use the sintaxis as described in this thread: MATCH AGANIST

Community
  • 1
  • 1
0

You can use a union for it:

select column from table where column like ('%What%') and column like ('%name%')
union
select column from table where (column like ('%What%') or column like ('%name%')) and column not in (select column from table where column like ('%What%') and column like ('%name%'))
Jens
  • 60,806
  • 15
  • 81
  • 95
  • Thank you that's what I wanted.... Now when I have more words, is there a simpler way to take care of all possible combinations.... other than generating a statement like this using recursion/loops?? – Karthik Shetty Jun 11 '15 at 09:19
  • @KarthikShetty Not that i know at the moment. – Jens Jun 11 '15 at 10:54