doudengjin8251 2014-01-22 09:39
浏览 95
已采纳

使用Mysql进行文本搜索

I am trying to create a search blog system where blogs are displayed based on user suggestions. Suggestions are basic hashtags which is stored in user_information table. While blogs will also have some hashtags input by blogger which will be stored in blog table.

Now as I have to match hashtags from user_information table to blog table, I am not finding a way to do this.

I have tried mysql LIKE CLAUSE but I am not able to get even single result.

My Query was [just representation]

SELECT * FROM blog WHERE hashtags LIKE $hashtags_from_user

  • 写回答

2条回答 默认 最新

  • doubipeng1336 2014-01-22 10:02
    关注

    You are not correctly escaping the value, you require the hash tags to be enclosed in quotations

    "LIKE '". $hashtags_from_user ."'";
    

    Additionally you will pay the price in the SELECT statement when you have denormalized database (hash-tags should have their own row in a hash_tags table and the blog would references these via a many-to-many or many-to-one association)

    Currently the output of the query is:

    SELECT * FROM blog WHERE hashtags LIKE '#hashtag1,#hashtag2,#hashtag'
    

    This isn't going to give you the results you want if you need to match on the individual tags.

    The only soultion would be to read the users has tag string, break it up into each tag (using $tags = explode(',', $hash_tags_from_user); and then build up a query to include all of them

    $where  = array();
    $select = 'SELECT * FROM foo';
    foreach($tags as $tag) {
      $where[] = sprintf('tag LIKE \'%s\'', $tag);
    }
    if (! empty($where)) $select .= 'WHERE ' . implode(' OR ', $where);
    

    Please don't use the above as it's just and example (and open to simple SQL injection) consider using prepared statements, with PDO/MySQLi.

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

报告相同问题?

悬赏问题

  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历