简体   繁体   English

sql字段值包含在字符串中

[英]sql field value is contained by string

I have a table in my database with the structure something like that: ID , category , county , keyword 我的数据库中有一个表,其结构类似于: IDcategorycountykeyword

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 我想从我的数据库中获取具有我所拥有的类别,县的行,但是'keyword'必须在我的文本中

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

Can anyone help me with the third condition ( 'keyword' must be in my $text )? 任何人都可以帮助我解决第三个问题( 'keyword'必须在我的$text )? Thank you! 谢谢!

If you mean the text of column keyword should be contained anywhere in the $text variable, then use 如果您的意思是列keyword的文本应该包含在$ text变量中的任何位置,那么请使用

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. 实际上,为了防止SQL注入,最好使用$category$county$text参数的预准备语句来防止SQL注入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM