Pergunta

Starting off, I apologize for bad table structure, it was not my decision and existed before me.

Anyway, I have a table tbl_cities that is a list of, you guessed it, cities (but for whatever reason are stored in column [desc]). I wanted to make a nifty AJAX text input that, as you type, it tries to find out what cities you are typing and offer them as suggestions. It's kinda like this example. So my query looked like this

SELECT [desc] FROM tbl_cities WHERE [desc] LIKE 'phil%'

Which works fine. However, I see there are a bunch of misspellings in this table, so I want to add fuzzy logic. I want to keep getting cities that match the first letters they type, so I have this query.

SELECT [desc] FROM tbl_cities WHERE [desc] LIKE 'phil%'
OR DIFFERENCE('phil', [desc])>3

Now I want to sort based on the [desc] LIKE 'phil%' before the fuzzy logic part. So in this example, Philadelphia should appear before random cities like PAOLA and POWELL

Foi útil?

Solução

I would try something like that :

order by case when [desc] like 'phil%' then 0 else 1 end, [desc]
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top