多个单词的反向匹配

发布于 2024-09-28 11:52:06 字数 323 浏览 5 评论 0原文

感谢您阅读本文。希望你能帮助我。

当我有一个包含这些行值的 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 技术交流群。

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

发布评论

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

评论(3

栩栩如生 2024-10-05 11:52:06

嵌套替换,无子查询。

SELECT id, search
FROM  a 
WHERE LENGTH( TRIM( 
REPLACE( REPLACE( REPLACE( 
CONCAT(  ' ', search,  ' ' ) ,  
' butterflies ',  ' ' ) ,  ' of ',  ' ' ) ,  ' america ',  ' ' ) ) ) = 0

        id  search
        1   butterflies
        2   america
        4   america butterflies

我在要搜索的单词中添加了书尾空格,以确保您不会与单词中间匹配(例如“咖啡”中的“of”)。此外,我在 search 短语中添加了空格书挡,以说明第一个和最后一个单词。

Nested replaces, no subqueries.

SELECT id, search
FROM  a 
WHERE LENGTH( TRIM( 
REPLACE( REPLACE( REPLACE( 
CONCAT(  ' ', search,  ' ' ) ,  
' butterflies ',  ' ' ) ,  ' of ',  ' ' ) ,  ' america ',  ' ' ) ) ) = 0

        id  search
        1   butterflies
        2   america
        4   america butterflies

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.

话少情深 2024-10-05 11:52:06

这是我尝试过的一种方法(虽然不是很有效):

select search, replace(filtered, 'butterflies', '') as filtered from (
   select search, replace(filtered, 'of', '') as filtered from (
      select search, replace(search, 'america', '') as filtered from table_name a
   ) b
) c;

这个查询将为您提供如下所示的内容:

+---------------------+----------+
| search              | filtered |
+---------------------+----------+
| butterflies         |          |
| america             |          |
| birds of america    | birds    |
| america butterflies |          |
+---------------------+----------+

完成这项工作的最后一部分给我带来了一些麻烦,但是......您需要一个将返回的 where 子句所有“空”行(即仅包含空白字符)。

这将过滤掉第三行并返回您想要的结果集。然而,我无法使用trim()让它工作,我不知道为什么。

例如,我尝试过:

where length(trim(c.filtered)) = 0;

这没有给我想要的结果集。我现在没有时间研究这个问题,但我想提一下这种方法,以防其他人想插话并完成谜题的解决。如果没有,我会尝试在今天晚些时候或明天进一步研究这个问题。

Here is one method that I experimented with (although not very efficient):

select search, replace(filtered, 'butterflies', '') as filtered from (
   select search, replace(filtered, 'of', '') as filtered from (
      select search, replace(search, 'america', '') as filtered from table_name a
   ) b
) c;

This query will give you something like the following:

+---------------------+----------+
| search              | filtered |
+---------------------+----------+
| butterflies         |          |
| america             |          |
| birds of america    | birds    |
| america butterflies |          |
+---------------------+----------+

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:

where length(trim(c.filtered)) = 0;

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.

中二柚 2024-10-05 11:52:06
SELECT * 
FROM table_name 
WHERE search LIKE '%butterflies%' 
  AND search LIKE '%of%' 
  AND search LIKE '%america%';

或者

SELECT * 
FROM table_name 
WHERE search REGEXP 'butterflies|of|america'; // not working

如果我没有遗漏一些东西:)

编辑:我遗漏了一些东西:(

SELECT * 
FROM table_name 
WHERE search LIKE '%butterflies%' 
  AND search LIKE '%of%' 
  AND search LIKE '%america%';

or

SELECT * 
FROM table_name 
WHERE search REGEXP 'butterflies|of|america'; // not working

If i am not missing something :)

Edit: I was missing something :(

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