简体   繁体   English

在mysql数据库的多个字段中搜索关键字

[英]Search for keywords in more than one fields of mysql database

I want to perform a site search. 我想进行网站搜索。 The thing is that I have 3 columns I want to search, and a row will be displayed in the results if the search query is any of these fields. 问题是我想搜索3列,如果搜索查询是这些字段中的任何一个,结果中将显示一行。 By which I mean that if someone searches "keyword1 keyword2 keyword3", the columns we search are page name, title and description, so if keyword1 was only in the title, keyword2 was only in the name, and keyword3 was only in the description, that would be a valid result. 我的意思是,如果有人搜索“keyword1 keyword2 keyword3”,我们搜索的列是页面名称,标题和描述,因此如果keyword1仅在标题中,则keyword2仅在名称中,而keyword3仅在描述中,这将是一个有效的结果。

I just can't figure out the logic behind this. 我无法弄清楚这背后的逻辑。 I tried using regex in the sql with the OR operator, but this still required all the keywords in either the title, name or description. 我尝试在带有OR运算符的sql中使用regex,但这仍然需要标题,名称或描述中的所有关键字。 Any idea how I accomplish this? 知道我是如何做到这一点的吗?

Please look at MySQL LIKE IN()? 请查看MySQL LIKE IN()?

You can replace the spaces with | 您可以用|替换空格 using str_replace and build a query like: 使用str_replace并构建一个类似的查询:

SELECT * from mytable where pagename REGEXP 'keyword1|keyword2|keyword3' and title REGEXP 'keyword1|keyword2|keyword3' and description REGEXP 'keyword1|keyword2|keyword3';

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

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