douxuanwei1980 2014-06-29 22:33
浏览 50
已采纳

在mysqli中搜索一列中的多个值,返回匹配的另一个值,按出现次数排序?

Before I start, here is the sample format of the database:

id  date         title            content
--------------------------------------------------------------
1   YYYY-MM-DD   some text here   lots of stuff to search here
2   YYYY-MM-DD   some text here   lots of stuff to search here
3   YYYY-MM-DD   some text here   lots of stuff to search here

I currently have something set up to get the id of a row, then assign the date, title, and content to PHP variables to display in different places on a page (it's a blog, these are posts).

Now I'm trying to make a function that will search the content for each string in an array (the code already splits words entered into the search box by spaces into an array, so entering cat hotdog suit will give [0]cat [1]hotdog [2]suit).

I have a loop set up that performs a query on each of the strings:

foreach ($queryArray as $queryArrayString) {
    // query the content column for $queryArrayString
}

However, I can't figure out what query I should use to find the id of the post that the string occurs in. I'd like to assign it to a variable ($results). After reversing the array, so higher ids (newer results) show up first, I'd like to find the titles of each id and store it like this:

id (let's say it is 3) => matching title
id (let's say it is 1) => matching title

Is this possible? I've tried several different queries, and none worked, but one took 2 minutes and loads of memory to attempt.

Any help would be appreciated.

EDIT: I've created a text index of the title and content columns (I'd like to search both), shown in this image.

Is it possible to search this? Like I've said in the comments, I'd just need it to return the corresponding id and title somehow. (Title for result link text, id for link destination: <a href="blog?p=[id]">[title]</a> is the format I'm using

  • 写回答

1条回答 默认 最新

  • drl959975 2014-06-30 00:50
    关注

    In ideal world, there are complex solutions available like Apache SOLR or Elasticsearch for indexing records and return efficiently when multiple text-search-terms are provided. Luckily, you can achieve such results by trying something like this (not tested)..

    $searchTerms = "cat hotdog suit";
    $searchArray = explode(' ',$searchTerms);
    
    $conditions = array();
    $foreach (searchArray as $searchTerm){
        $conditions[] = 'title-content LIKE %'.searchTerm.'%';
    }
    
    $query = '
    SELECT
        tableName.id,
        CONCAT_WS(' ', tableName.title,tableName.content) as title-content
    FROM
        tableName
    WHERE
        '.implode(' OR ', $conditions);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在vue.config.js中读取到public文件夹下window.APP_CONFIG.API_BASE_URL的值
  • ¥50 浦育平台scratch图形化编程
  • ¥20 求这个的原理图 只要原理图
  • ¥15 vue2项目中,如何配置环境,可以在打完包之后修改请求的服务器地址
  • ¥20 微信的店铺小程序如何修改背景图
  • ¥15 UE5.1局部变量对蓝图不可见
  • ¥15 一共有五道问题关于整数幂的运算还有房间号码 还有网络密码的解答?(语言-python)
  • ¥20 sentry如何捕获上传Android ndk 崩溃
  • ¥15 在做logistic回归模型限制性立方条图时候,不能出完整图的困难
  • ¥15 G0系列单片机HAL库中景园gc9307液晶驱动芯片无法使用硬件SPI+DMA驱动,如何解决?