在 MySQL 中的文本列中搜索字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2526772/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Search for string within text column in MySQL
提问by user94154
I have mysql table that has a column that stores xml as a string. I need to find all tuples where the xml column contains a given string of 6 characters. Nothing else matters--all I need to know is if this 6 character string is there or not.
我有一个 mysql 表,它有一列将 xml 存储为字符串。我需要找到 xml 列包含 6 个字符的给定字符串的所有元组。其他都不重要——我只需要知道这个 6 个字符的字符串是否存在。
So it probably doesn't matter that the text is formatted as xml.
因此,文本格式为 xml 可能无关紧要。
Question: how can I search within mysql?
ie
SELECT * FROM items WHERE items.xml [contains the text '123456']
问题:如何在mysql中搜索?IE
SELECT * FROM items WHERE items.xml [contains the text '123456']
Is there a way I can use the LIKE operator to do this?
有没有办法可以使用 LIKE 运算符来执行此操作?
回答by Mike Cialowicz
You could probably use the LIKE
clauseto do some simple string matching:
您可能可以使用该LIKE
子句进行一些简单的字符串匹配:
SELECT * FROM items WHERE items.xml LIKE '%123456%'
If you need more advanced functionality, take a look at MySQL's fulltext-search functions here: http://dev.mysql.com/doc/refman/5.1/en/fulltext-search.html
如果您需要更高级的功能,请在此处查看 MySQL 的全文搜索功能: http://dev.mysql.com/doc/refman/5.1/en/fulltext-search.html
回答by Raj
Using like might take longer time so use full_text_search:
使用 like 可能需要更长的时间,所以使用full_text_search:
SELECT * FROM items WHERE MATCH(items.xml) AGAINST ('your_search_word')
回答by Amy B
SELECT * FROM items WHERE `items.xml` LIKE '%123456%'
The %
operator in LIKE
means "anything can be here".
将%
在运营商LIKE
的意思是“什么都可以在这里”。
回答by systempuntoout
Why not use LIKE?
为什么不使用 LIKE?
SELECT * FROM items WHERE items.xml LIKE '%123456%'
回答by rytis
you mean:
你的意思是:
SELECT * FROM items WHERE items.xml LIKE '%123456%'
回答by Debbie Kurth
When you are using the wordpress prepare line, the above solutions do not work. This is the solution I used:
当您使用 wordpress prepare 行时,上述解决方案不起作用。这是我使用的解决方案:
$Table_Name = $wpdb->prefix.'tablename';
$SearchField = '%'. $YourVariable . '%';
$sql_query = $wpdb->prepare("SELECT * FROM $Table_Name WHERE ColumnName LIKE %s", $SearchField) ;
$rows = $wpdb->get_results($sql_query, ARRAY_A);