多个单词的反向匹配
感谢您阅读本文。希望你能帮助我。
当我有一个包含这些行值的 Mysql 表时
id|搜索
========
1|蝴蝶
2|美国
3|美洲鸟类
4| america butterflies
如何判断哪些行包含“search”列中出现在字符串“butterflies of america”中的所有单词,无论搜索单词的数量或顺序如何。
(我想在这个例子中检索1,2和4) 我现在使用编码循环来解决这个问题,如果使用 mysql 更有效地修复它,那就太好了。我尝试了全文搜索和正则表达式,但完全卡住了。德克萨斯州。
Thanks for reading this. Hope you can help me.
When I have a Mysql table with these row values
id| search
========
1| butterflies
2| america
3| birds of america
4| america butterflies
how can I tell which rows have all the words in column 'search' occuring in the string "butterflies of america", regardless of the number or order of the search words.
(I would like to retrieve 1,2 and 4 in this example)
I now use a coded loop to solve this problem, would be nice to fix it more efficient with mysql. I tried full text search and regular expressions but are complety stuck. Tx.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
嵌套替换,无子查询。
我在要搜索的单词中添加了书尾空格,以确保您不会与单词中间匹配(例如“咖啡”中的“of”)。此外,我在
search
短语中添加了空格书挡,以说明第一个和最后一个单词。Nested replaces, no subqueries.
I added bookending spaces to the words to search for to ensure you don't match against the middle of words (e.g. 'of' in 'coffee'). Moreover, I added space bookends to the
search
phrase to account for the first and last words.这是我尝试过的一种方法(虽然不是很有效):
这个查询将为您提供如下所示的内容:
完成这项工作的最后一部分给我带来了一些麻烦,但是......您需要一个将返回的 where 子句所有“空”行(即仅包含空白字符)。
这将过滤掉第三行并返回您想要的结果集。然而,我无法使用trim()让它工作,我不知道为什么。
例如,我尝试过:
这没有给我想要的结果集。我现在没有时间研究这个问题,但我想提一下这种方法,以防其他人想插话并完成谜题的解决。如果没有,我会尝试在今天晚些时候或明天进一步研究这个问题。
Here is one method that I experimented with (although not very efficient):
This query will give you something like the following:
The last piece to make this work was giving me some trouble, though... you need a where clause which will return all rows that are "empty" (i.e. contain only whitespace characters).
That will filter out the third row and return the result set you desire. However, I wasn't able to get this to work using trim() and I don't know why.
For example, I tried:
This did not give me the result set I wanted. I don't have anymore time to look into this right now, but I wanted to mention this approach in case someone else wants to chime in and finish solving the puzzle. If not, I will try to look into this a little more later today or tomorrow.
或者
如果我没有遗漏一些东西:)
编辑:我遗漏了一些东西:(
or
If i am not missing something :)
Edit: I was missing something :(