MySQL-需要一个字符串中最大匹配字母的搜索结果 [英] MySQL - Need search result of maximum matching letters from a string
问题描述
您好,我正在编写自己的MySQL查询,其中需要如下记录结果.
Hi I am writing my own MySQL query where I need a result of records as follows.
表格中的单词-ABC XYZ
我的字符串-ABC XYZQWER
My string - ABC XYZQWER
当我按如下所示运行查询时-
when I ran my query as below -
SELECT * FROM myTABLE where `column` LIKE 'ABC XYZQWER%';
我得到的结果是空的.我知道MySQL LIKE与字符串的结果匹配.
I am getting empty result. I am aware of the fact that MySQL LIKE matches the result of string.
我需要一种解决办法.
我使用"ABC X"进行搜索-它给了我正确的搜索结果.
I I searched it using 'ABC X' - it is giving me a proper result.
推荐答案
您可以使用函数LOCATE()
:
SELECT `column`
FROM myTable
WHERE LOCATE(`column`, 'ABC XYZQWER') = 1;
只要在名为column
的列中存在值ABC XYZ
,查询的结果将至少为:
As long as there is a value ABC XYZ
in the column named column
, the result of the query will be at least:
+---------+
| column |
+---------+
| ABC XYZ |
+---------+
查找内部匹配项
可以使用比较运算符>=
在搜索字符串'ABC XYZQWER'
中查找匹配的字符串,例如'BC'
.因此WHERE子句将如下所示:
Finding a matching string like 'BC'
, which is inside the search string 'ABC XYZQWER'
, is possible by using the compare operator >=
. So the WHERE clause will look like this:
WHERE LOCATE(`column`, 'ABC XYZQWER') >= 1;
这篇关于MySQL-需要一个字符串中最大匹配字母的搜索结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!