如何在 MySQL 的两个不同表中选择相似的行(可能吗?)
我有两张桌子,我想获取所有拼写相似的学校或同一所学校的学校。例如:
my_table_a
学校
:
Olde School
New School
Other, C.S. School
Main School
Too Cool for School
my_table_b:
School
Old School
New ES
Other School
Main School
Hardknocks School
是否可以编写一个 SELECT 查询来查找两个表中拼写相似的学校。有没有办法在列上使用 LIKE 或通配符?
例如:
SELECT my_table_a.school, my_table_b.school
FROM ` my_table_a` , my_table_b
WHERE my_table_a.directory_school_name_09_10 LIKE my_table_b.school
我用真实的表尝试了上述语句,我只是得到了“=”会产生的结果。
基本上,我想获取每个表列中的前 4 所学校。 (当然,在现实世界中,我不会知道前 4 所学校是相似的)。
我想做的事情可能吗?
I've got two tables and I'd like to grab all of the schools where there is a similarly spelled school or the same school. For example:
my_table_a:
School
Olde School
New School
Other, C.S. School
Main School
Too Cool for School
my_table_b:
School
Old School
New ES
Other School
Main School
Hardknocks School
Is it possible to write a SELECT query that will find the similarly spelled schools in the two tables. Is there a way to use LIKE or wildcards on columns?
Something such as:
SELECT my_table_a.school, my_table_b.school
FROM ` my_table_a` , my_table_b
WHERE my_table_a.directory_school_name_09_10 LIKE my_table_b.school
I tried the above statement with my real tables and I simply got what '=' would have produced.
Basically, I want to grab the first 4 schools in each table's column. (Of course, in the real world, I won't know that the first 4 schools are similar).
Is what I'm trying to do even possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于 Levenshtein Distance 算法的 UDF 实现,您可能需要查看“codejanitor.com:Levenshtein 距离作为 MySQL 存储函数”:
现在让我们使用您在问题中提供的数据构建一个测试用例:
然后:
显然返回学校名称完全匹配的匹配项:
现在我们可以尝试使用
LEVENSHTEIN
函数来返回 编辑距离 等于或小于 2 个字符的学校名称:现在使用
< = 3
作为编辑距离阈值:我们得到以下结果:
注意这次
Olde School
也匹配Other School
和New School< /code> 也匹配
Old School
。这些可能是误报,并且表明定义阈值对于避免不正确的匹配非常重要。解决此问题的一种常见技术是在应用阈值时考虑字符串的长度。事实上,我引用的网站为此实现还提供了一个
LEVENSHTEIN_RATIO
函数,该函数返回基于字符串长度的编辑差异的比率(以百分比形式)。For a UDF implementation of the Levenshtein Distance algorithm you may want to check out "codejanitor.com: Levenshtein Distance as a MySQL Stored Function":
Now let's build a test case, using the data you provided in your question:
Then:
Obviously returns a match where the school names match exactly:
Now we can try to use the
LEVENSHTEIN
function to return school names that have an edit distance of 2 characters or less:Now using
<= 3
as an edit distance threshold:We get the following result:
Note how this time
Olde School
also matchedOther School
, andNew School
matchedOld School
as well. These are probably false positive, and shows that defining the threshold is very important to avoid incorrect matches.One common technique to tackle this problem is to take into consideration the length of the strings when applying a threshold. In fact, the site that I cited for this implementation also provides a
LEVENSHTEIN_RATIO
function which returns the ratio (as a percentage) of the edit difference based on the length of the strings.您可以尝试比较调用 SOUNDEX。
或者您可以比较编辑距离。
You can try comparing the result of calling SOUNDEX.
Or you can compare the Levenshtein distance.
这个问题相当广泛。什么才算相似?不管怎样,虽然我不能给你一个确切的答案,但我可以说你可能想使用全文搜索: http://dev.mysql.com/doc/refman/5.1/en/fulltext-search.html 或使用某种使用 LIKE 通配符运算符的技术: %
也会匹配“new school”和“new darn school”。
This question is pretty broad. What qualifies as similar? Anyway, while I can't give you an exact answer, I can say that you'll probably want to use a fulltext search: http://dev.mysql.com/doc/refman/5.1/en/fulltext-search.html or use some technique using the LIKE wildcard operator: %
will match "new school" and "new darn school" too.
你可以尝试这个,
只需根据你的表名和字段名输入即可。
You can try this one
just put the table name and field name according to your one.