MySQL:将“LIKE”转换为“LIKE”搜索全文?
我对 MySQL 有一个非常简单的 LIKE 搜索,我想将其转换为全文。问题是我需要能够实现它,以便它以 X 开头。如下例所示:
SELECT column FROM table WHERE column LIKE "startswith%"
如您所见,查询返回以“startswith”开头的所有结果。我需要用全文来做到这一点。
这可以吗?
I have a pretty simple LIKE search for MySQL that i'd like to transform into a fulltext. The problem is i need to be able to implement it so that it starts with X. Like the example below:
SELECT column FROM table WHERE column LIKE "startswith%"
as you can see that query returns all results that begins with "startswith". I need to do this with a fulltext.
Is this possible to do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,这不是全文的工作原理(它实际上只是一个下面有松散单词的列表,没有有关相对于字符串的位置的信息),但没有理由不能使用
LIKE ...
作为额外的WHERE
子句。如果您在column
上没有其他键,FULLTEXT 仍然可以帮助获得较小的结果子集。如果您确实在列
上有一个键,则使用 FULLTEXT 是没有用的。您可以使用
ADD INDEX (column(123));
在列的开头设置一个键(这只会索引前 123 个字符)。这也适用于文本/blob 列(在后一种情况下,它是您给出的二进制长度)。No, that isn't how fulltext works (it's actually just a list with loose words underneath, no information about location relative to the string) but there's no reason why you can't have that
LIKE ...
as an extraWHERE
clause. FULLTEXT can still help to get a smaller subset of results if you haven't got another key oncolumn
. If you do have a key oncolumn
, using FULLTEXT for this is useless.You can set a key on just the start of a column with
ADD INDEX (column(123));
(which would only index the first 123 characters). THis also works for text/blob columns (in the latter case it's the binary length you give).我不确定 MySQL,但在 SQL Server 中,您可以将列转换为
varchar
并对结果执行LIKE
,如下所示:因为这是 ANSI 标准,我想 MySQL 也会处理这个问题。
I am not sure about MySQL, but in SQL Server you can convert the column into a
varchar
and perform aLIKE
on the result, such as below:Since this is ANSI standard, I presume MySQL will handle this as well.