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

报告相同问题?

悬赏问题

  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法