duanguanzai6181
2014-02-11 14:05
浏览 28
已采纳

sql字段值包含在字符串中

I have a table in my database with the structure something like that: ID,category,county,keyword

Now I have the category, the county and some text.

I want to take from my database the rows with the category, county that I have, BUT also the 'keyword' must be in my text

select from `posts` where `category`='$category' and `county`='$county'

Can anyone help me with the third condition ('keyword' must be in my $text)? Thank you!

图片转代码服务由CSDN问答提供 功能建议

我的数据库中有一个表格,其结构类似于: ID category county keyword

现在我有了类别,县和一些文本。

我想从我的数据库中获取具有我所拥有的类别,县的行,但'关键字'也必须在我的文本中

 从`posts`中选择`category` ='$ category'和`county` ='$ county'
   
 
 

任何人都可以帮我解决第三个问题('keyword'必须在我的 $ text 中)? 谢谢!

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongzhun8449 2014-02-11 14:08
    已采纳

    If you mean the text of column keyword should be contained anywhere in the $text variable, then use

    select ID,category,county,keyword
      from `posts`
     where `category`='$category' and `county`='$county'
       and '$text' like concat('%', `keyword`, '%')
    

    Actually, to prevent SQL injection, it would be better to use a prepared statement using parameters for $category, $county, and $text to prevent SQL injection.

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题