Prepending an * (asterisk) to a Fulltext Search in MySQL(在 MySQL 中的全文搜索前添加 *(星号))
问题描述
我知道星号是可以附加到全文搜索词末尾的通配符,但是如果我搜索的关键字是后缀怎么办?例如,我希望能够搜索ames"并返回包含名称james"的结果.这是我当前的查询,它不起作用,因为您不能在全文搜索前添加星号.
I understand that the asterisk is a wildcard that can be appended to the end of fulltext search words, but what if my searched keyword is a suffix? For example, I want to be able to search for "ames" and have a result that contains the name "james" returned. Here is my current query which does not work because you cannot prepend asterisks to fulltext searches.
SELECT * FROM table WHERE MATCH(name, about, address) AGAINST ("*$key*" IN BOOLEAN MODE)
我会简单地切换到使用 LIKE,但对于我的数据库大小来说它太慢了.
I would simply switch to using LIKE, but it would be way too slow for the size of my database.
推荐答案
你可以做的是在你的数据库中创建另一个带有全文搜索索引的列,这个新列应该有你想要的列的反转字符串搜索,您将反转搜索查询并使用它在反转的列上进行搜索,以下是查询的样子:
What you could do is create another column in your database with full-text search index, this new column should have the reversed string of the column you are trying to search on, and you will reverse the search query and use it to search on the reversed column, here is how the query will look like:
SELECT * FROM table WHERE MATCH(column1) AGAINST ("$key*" IN BOOLEAN MODE) OR MATCH(reversedColumn1) AGAINST ("$reveresedkey*" IN BOOLEAN MODE)
第一个条件
MATCH(column1) 反对(布尔模式中的$key*")
例子:reversedColumn1==>Jmaes
$reveresedkey*==>ames*
将搜索以 ames ==> 不匹配的单词开头的单词the first condition
MATCH(column1) AGAINST ("$key*" IN BOOLEAN MODE)
example:reversedColumn1==>Jmaes
$reveresedkey*==>ames*
will search for words that start with ames ==> no match秒条件
MATCH(reversedColumn1) AGAINST ("$reveresedkey*" IN BOOLEAN MODE)
例子:reversedColumn1==>semaJ
$reveresedkey*==>sema*
将搜索以 ames 结尾的单词 ==> 我们有匹配项the seconds condition
MATCH(reversedColumn1) AGAINST ("$reveresedkey*" IN BOOLEAN MODE)
example:reversedColumn1==>semaJ
$reveresedkey*==>sema*
will search for words that end with ames ==> we have a match如果你的文字很短,这可能不是一个坏主意:
This might not be a bad idea if your text is short:
这篇关于在 MySQL 中的全文搜索前添加 *(星号)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 MySQL 中的全文搜索前添加 *(星号)
基础教程推荐
- SQL Server 2016更改对象所有者 2022-01-01
- SQL Server:只有 GROUP BY 中的最后一个条目 2021-01-01
- 如何在 SQL Server 的嵌套过程中处理事务? 2021-01-01
- Sql Server 字符串到日期的转换 2021-01-01
- 将数据从 MS SQL 迁移到 PostgreSQL? 2022-01-01
- 无法在 ubuntu 中启动 mysql 服务器 2021-01-01
- 在 VB.NET 中更新 SQL Server DateTime 列 2021-01-01
- SQL Server 中单行 MERGE/upsert 的语法 2021-01-01
- ERROR 2006 (HY000): MySQL 服务器已经消失 2021-01-01
- 使用pyodbc“不安全"的Python多处理和数据库访问? 2022-01-01