gpt4 book ai didi

mysql - SQL以任意顺序搜索单词

转载 作者:可可西里 更新时间:2023-11-01 07:48:45 24 4
gpt4 key购买 nike

给定这个模型:

+-----------+-------------+---------------------------+
| item_id | item_name | desc |
+-----------+-------------+---------------------------+
| 1 | Product 1 | lorem ipsum dolor... |
| 2 | Product 2 | lorem mauris eu... |
| 3 | Product 3 | scelerisque sagittis... |
| 4 | Product 4 | lorem dolor ipsum... |
| 5 | Product 5 | ipsum dolor lorem... |
+-----------+-------------+---------------------------+

我想在 item_namedesc 中搜索所有包含单词 lorem ipsum 的产品。此外,任何单词都可以出现在 loremipsum 之间,并且 loremipsum 可以以任何顺序出现。基本上,此搜索将返回项目 145

现在,我知道我可以通过以下方式完成此任务:

SELECT * FROM items
WHERE (item_name LIKE 'lorem%ipsum'
OR desc LIKE 'lorem%ipsum')
OR (item_name LIKE 'ipsum%lorem'
OR desc LIKE 'ipsum%lorem')

但如果我的搜索词较长(即 lorem ipsum dolor sit amet,consectetur adipiscing elit),我觉得随着 OR 添加到查询中,它可能会变得有点荒谬。有没有更简单/更有效的方法来处理这个问题?

最佳答案

这种搜索要求听起来很适合 full text search .

与传统的 SQL like 搜索相比,全文搜索更像是(或至少可以是)类似搜索的“搜索引擎”。对于全文搜索,搜索词的顺序无关紧要,并且根据 RDBM,一些全文搜索功能允许同义词查找以及噪声词过滤。

在(我相信)大多数情况下,全文搜索比like 搜索要快得多。 Here是一篇关于在 mysql 中进行全文搜索入门的文章。

示例 mySql 全文搜索语法:

select *
from items
where match(item_name) against ('+lorem +ipsum' in boolean mode)

全文搜索确实有一定的要求(在文章的链接中有详细说明)。我个人没有使用过 mysql 的全文搜索,或者我会列出步骤。如果您想朝那个方向前进,应该足以让您入门。

关于mysql - SQL以任意顺序搜索单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32873328/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com