dongqian0763 2015-09-06 17:28
浏览 61
已采纳

MySQL:使用单个输入搜索多个列(GoogleMaps样式)

I have a database loaded with geonames's data for the US. I want to perform a query on that table to find the closest match from a single input that has a more or less the same functionality as Google Maps (i.e. the user may input a zip code, a comma separated hierarchical address like 'Nevada, Las Vegas' or a combination of both).

So far I've added FULLTEXT indexes to the relevant fields of the table, and performing a query from the input like this:

SELECT * FROM places WHERE MATCH(country_code, postal_code, place_name, admin_name1, admin_code1, admin_name2, admin_code2) AGAINST (?);

Where ? is replaced by the input by a php script. Altough it kinda works, it does not perform as expected, it doesn't put the most relevant matches first and needs a full word to start finding matches (I tried with wildcards but got worse). I also tried splitting the input if it had commas but the logic of the query started to get messy and couldn't produce a better performing script.

Is there a way to search this database in such broad terms for matches?

I'm querying the database via AJAX so I need it to start searching even if the word is not completed yet (I'm using jQuery's typeahead).

Thanks in advance!

  • 写回答

3条回答 默认 最新

  • dongxian0320 2015-09-06 17:38
    关注

    Replace string_to_be_searched text with your text.

    SELECT * FROM places WHERE 
    instr(concat_ws(" ",country_code, postal_code, place_name, admin_name1,
    admin_code1, admin_name2, admin_code2),"string_to_be_searched")>0;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?