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条)

报告相同问题?

悬赏问题

  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥30 用arduino开发esp32控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿