MySql 正则表达式选择与存储过程中的(动态)多个值匹配的列

发布于 2024-09-25 20:23:10 字数 613 浏览 6 评论 0原文

我正在尝试生成一个查询,在其中我想要选择与多个值匹配的列(文本)。

例如:我有两列,id 和description。假设我的第一行包含带有值的描述列

Google 是一个很棒的网站 搜索引擎

,第二行带有描述列值

亚马逊网站是一个很棒的电子商务网站 商店

我创建了一个查询

从表名中选择*,其中 描述 REGEXP '网站 \| 搜索'

它返回同时包含 google 和 amazon 的行,但我只想返回 google,因为我想要那些同时包含单词 websitesearch 的行以及数字要匹配的单词也不固定,基本上我创建的查询是用于搜索下拉列表,

传递的所有单词都应该出现在列中,列中出现的单词的顺序并不重要。如果除了使用 regex 之外还有其他更好的选择,请指出。

编辑:传递的单词数量是动态的且未知,用户可以传递其他单词以与列进行匹配。我将在存储过程中使用查询

I am trying to generate a query where I want to select columns(text) matching multiple values.

eg: I have two columns, id and description. suppose my first row contains description column with value

Google is website and an awesome
search engine

, second row with description column value

Amazon website is an awesome eCommerce
store

I have created a query

Select * from table_name where
description REGEXP 'Website \|
Search'

It returns both the rows i.e with both google and amazon, but i want to return only google as i want those rows with both the words website and search also the number of words to be matched is also not fixed, basically the query I am creating is for a search drop down,

All the words that are passed should be present in the column, the order of the words present in the column is not important. If there are other better options besides using regex , please do point out.

Editing: the number of words that are passed are dynamic and not known, the user may pass additional words to be matched against the column. I would be using the query within a stored Procedure

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白芷 2024-10-02 20:23:10

从性能的角度来看,真的不认为正则表达式解决方案对您有好处。认为您应该寻找全文搜索。

具体来说,您需要在表定义中使用类似以下内容创建全文索引:

create table testTable
(
Id int auto_increment not null,
TextCol varchar(500)
fulltext(TextCol)
); 

然后您的查询变得更容易:

select * from testTable where Match(TextCol) against ('web') 
AND Match(TextCol) against ('server')

强烈建议您阅读有关全文匹配的 MySQL 文档,其中有很多小技巧和功能在此任务中很有用(包括运行上面查询的更有效的方法)

编辑:也许布尔模式将帮助您获得像这样的简单解决方案:

Match(textCol) against ('web+ Server+' in boolean mode)

您所要做的就是构建反对字符串,所以我认为这可以在 SP 中完成,无需动态 SQL

Really don;t think the regex solution is going to be good for you from a performance point of view. Think you should be looking for FULL text searches.

Specifically you need to create a full text index with something like this in the table definition:

create table testTable
(
Id int auto_increment not null,
TextCol varchar(500)
fulltext(TextCol)
); 

Then your query gets easier:

select * from testTable where Match(TextCol) against ('web') 
AND Match(TextCol) against ('server')

Strongly suggest you read the MySQL docs regarding FULLTEXT matching and there are lots of little tricks and features that will be useful in this task (including more efficient ways to run the query above)

Edit: Perhaps Boolean mode will help you to an easy solution like this:

Match(textCol) against ('web+ Server+' in boolean mode)

All you have to do is build the against string so I think this can be done in an SP with out dynamic SQL

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文