dongyuan9292 2018-11-01 11:24
浏览 88
已采纳

使用排列在MySQL中搜索

I need help.
I have a table where only two columns are: ID and NAME and these data:

ID | NAME
1    HOME
2    GAME
3    LINK

And I want show e.g. row with name: HOME if user search: HOME or OMEH or EMOH or HMEO, etc... - all permutations from word HOME.

I can't save to mysql all these permutations and search in this columns, because some words will be a too big (9-10 chars) and more than 40 MB for each 9 chars words.

  • 写回答

2条回答 默认 最新

  • douciwang6819 2018-11-01 11:31
    关注

    One way to solve this problem is to store the sorted set of characters in each name in your database as an additional column and then sort the string the user inputs before searching e.g. database has

    ID   NAME   CHARS
    1    HOME   EHMO
    2    GAME   AEGM
    3    LINK   IKLN
    

    Then when searching in PHP you would do this:

    $search = 'MEHO';                // user input = MEHO
    $chars = str_split($search);
    sort($chars);
    $search = implode('', $chars);   // now contains EHMO
    $sql = "SELECT ID, NAME FROM table1 WHERE CHARS = '$search'";
    // perform query etc.
    

    Output

    ID   NAME
    1    HOME
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 eclipse连接sap后代码跑出来空白
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi