我正在使用此mysql查询从DB
获取数据 SELECT DISTINCT CONCAT( streetObj.street_type, ' ',streetObj.street_name, ', ', neighborhoodObj.name , ', ', cityObj.name, ', ', stateObj.abbreviation ) namet
FROM street streetObj
LEFT
JOIN cep cepObj1
ON cepObj1.street_id = streetObj.street_id
LEFT
JOIN neighborhood neighborhoodObj
ON neighborhoodObj.neighborhood_id = cepObj1.start_neighborhood_id
LEFT
JOIN city cityObj
ON streetObj.city_id = cityObj.city_id
LEFT
JOIN state stateObj
ON stateObj.state_id = cityObj.state_id
WHERE CONCAT(streetObj.street_type,streetObj.street_name) LIKE '%rua%'
AND CONCAT(streetObj.street_type,streetObj.street_name) LIKE '%Gomes%'
AND CONCAT(streetObj.street_type,streetObj.street_name) LIKE '%de%'
AND CONCAT(streetObj.street_type,streetObj.street_name) like '%Ca%'
AND cityObj.city_id = '9668'
ORDER
BY namet ASC
LIMIT 10;
当我输入
时执行此查询 rua Gomes de Ca
这个查询结果就是这个
Rua Baltazar Gomes de Alarcão, Jardim Miriam, São ...
Rua Cabo José Gomes de Barros, Conjunto Habitacion...
Rua Cabo Luís Gomes de Quevedo, Parque Novo Mundo,...
Rua Gomes de Carvalho, Vila Olímpia, São Paulo, SP
Rua João Gomes de Mendonça, Jaraguá, São Paulo, SP
Rua João Gomes de Mendonça, Jardim Taipas, São Pau...
Rua Pedro Gomes de Camargo, Vila Rio Branco, São P...
所以你可以看到我想要那些找到完全匹配的结果,但它不起作用。 在这个我想要的查询
Rua Gomes de Carvalho, Vila Olímpia, São Paulo, SP
在最高位置。
答案 0 :(得分:0)
您需要根据匹配的强度对结果进行排名,并按此排序。您必须自己定义排序。例如:
select ..
from...
ORDER BY
case
when text like "%all my search phrase%" then 1
when text like "%all my%" then 2
when text like "%search phrase%" then 2
when text like "%phrase%" then 3
else 1000 end
DESCENDING
或
ORDER BY
case when text like "%word%" then 1 else 0 end
+
case when text like "%second_word%" then 1 else 0 end
+
.....
DESC
特别针对您的示例
select namet from
(select 'Rua Baltazar Gomes de Alarcão, Jardim Miriam, São ...' as namet
union all select 'Rua Cabo José Gomes de Barros, Conjunto Habitacion...'
union all select 'Rua Cabo Luís Gomes de Quevedo, Parque Novo Mundo,...'
union all select 'Rua Gomes de Carvalho, Vila Olímpia, São Paulo, SP'
union all select 'Rua João Gomes de Mendonça, Jaraguá, São Paulo, SP'
union all select 'Rua João Gomes de Mendonça, Jardim Taipas, São Pau...'
union all select 'Rua Pedro Gomes de Camargo, Vila Rio Branco, São P...')tbl
order by
case when namet like "%rua gomes de ca%" then 100 else 0 end+ #high score for full match
case when namet like "%rua%" then 1 else 0 end+ #lower score for partial matches
case when namet like "%Gomes%" then 1 else 0 end+
case when namet like "%de%" then 1 else 0 end+
case when namet like "%ca%" then 1 else 0 end desc LIMIT 10
虽然你可能想要写一些东西来将你的搜索短语分成单词,但是搜索每个单词,并对匹配的单词数进行排名。您还可以查看soundex或levenstein距离以获得排名相似度。在sql中执行此操作比以编程方式执行更难。