two words and blank spaces not working in MYSQL query using LIKE

可紊 提交于 2019-12-02 10:39:27

Think of how your query will look at the end:

Select ... where '%two words%. ...

If you want to search for words like that, you'll have to massage the data to look more like:

 ... Like '%two%words%'
 ... Like '%two%' or like '%words%'

depending on your exact search requirements

Unless the two words are adjacent in the text, the LIKE operator won't find them. You may want to use full text search.

In order to find two non-contiguous words, the input would need to be split up into two separate values and the query would have to look something like this:

WHERE (titulo LIKE '%$word1%' OR intro LIKE '%$word1%' OR extra LIKE '%$word1%' OR
       titulo LIKE '%$word2%' OR intro LIKE '%$word2%' OR extra LIKE '%$word2%' )

That is assuming you want a match with either word. If both must match, then something like this:

WHERE (titulo LIKE '%$word1%' AND titulo like '%$word2%' OR
       intro LIKE '%$word1%' AND intro LIKE '%$word2%'  OR 
       extra LIKE '%$word1%' AND extra LIKE '%$word2%' )

And one other thing. It would be better to use parameterized queries to avoid an SQL injection.

where ( MATCH( titulo, intro, extra) AGAINST ('word1 word2' in BOOLEAN MODE))

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!