0

I have a table of location, that have more than 150 locations with area and zipcode columns. I am using query "Select area from locations where area like %n%", then i am getting results Nigdi Nigdi Nigdi Chinchwad Chinchwad Nasik

Here why i am getting Nasik in last, even my first search keyword is 'n'. And Nasik has the 'n' at first location. Can Somebody help please.

Rakesh Varma
  • 151
  • 1
  • 2
  • 14

2 Answers2

2

If you want to order by the position of "n" in the area, then you need to specify that. Remember, a SQL query returns an unordered set unless you explicitly include order by. So:

Select area
from locations
where area like '%n%'
order by instr(area, 'n'), area;
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • Fair enough, perhaps I closed too soon as it was unclear. I would probably recommend adding `, area` to the end of the `order by` though. – sgeddes Aug 17 '16 at 23:50
0

Your question isn't very clear but I'm guessin that you want one of the following:

SELECT area FROM locations WHERE area LIKE '%n%' ORDER BY area

or

SELECT area FROM locations WHERE area LIKE 'n%' ORDER BY area
John Wu
  • 50,556
  • 8
  • 44
  • 80
  • i tried with your second query. bt in this m not getting Chinchwad area. I want like Nasik, Nigdi, Nigdi, Chinchwad. Hope you understand my question now – Rakesh Varma Aug 17 '16 at 23:48