简体   繁体   中英

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. A basic select for a single key word in mysql can look like this;

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. I'm wondering if there's a way to do it on the mysql side? 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. 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).

PHP will make this much simpler, and you can also use prepared statements to make things more secure. See this discussion .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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