简体   繁体   English

使用MySQL搜索多个术语

[英]Searching for multiple terms with MySQL

I'm looking for the most effective way to do a search with select with multiple terms in mysql and php. 我正在寻找最有效的方法,以在mysql和php中使用带有多个术语的select进行搜索。 A basic select for a single key word in mysql can look like this; mysql中单个关键字的基本选择如下所示:

select * from <table> where (Message like '%term%' or Subject like '%term%');

Works fine for a single term. 可以正常工作一个学期。 For multiple terms, the suggestions that I've seen is that each word that's posted from a form should be sent to an array and the contents should be processed with a For loop. 对于多个术语,我看到的建议是,应将表单中张贴的每个单词发送到数组,并使用For循环处理内容。 I'm wondering if there's a way to do it on the mysql side? 我想知道是否有办法在mysql方面做到这一点? My suspicion is that there isn't, but it doesn't hurt to ask. 我的怀疑是没有,但问问并没有伤害。

You could do it strictly using MySQL, with lots of calls to MID() and so on, but it would be much more complex than in PHP. 您可以使用MySQL严格执行此操作,并调用MID()等等,但是它比PHP复杂得多。 In fact, you would almost certainly need a stored procedure, unless you were certain that the query only involved up to a fixed number of terms (eg, between 1 and 10 words). 实际上,几乎可以肯定需要一个存储过程,除非您确定查询只涉及固定数量的术语(例如1到10个单词)。

PHP will make this much simpler, and you can also use prepared statements to make things more secure. PHP将使这一过程变得更加简单,并且您还可以使用准备好的语句来使事情更安全。 See this discussion . 看到这个讨论

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

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