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 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制