sql 数据库可以返回 sql 结果集以及结果的分数吗?
只是好奇,如果我想将字符串发送到数据库(可能是 MS SQL Server),任何人都可以提供有关从数据库返回结果的最佳方法的任何见解,其中结果集可能会在其上进行排序和“评分”与传入的字符串的接近程度?
那么,如果我发送一个查询:,
SELECT name FROM table where name LIKE 'Jon'
然后得到 1000 个结果,如下所示:
100 Jon
98 John
80 Jonathan
32 Nathan
视图、索引、存储过程、编码解决方案?有什么建议?
Just curious, if I wanted to send strings to a database (perhaps for MS SQL Server) can anyone provide any insight on what the best way would be to return results from a database where the result set might be sorted and "scored" on its closeness to the string passed in?
So, if I sent a query for :
SELECT name FROM table where name LIKE 'Jon'
and then get a result of 1000 results that looks like:
100 Jon
98 John
80 Jonathan
32 Nathan
Views, indexes, stored procedures, coded solution? What is the recommendation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你可以,但你需要使用另一个函数来完成它。 Levenshtein 比率或 Jaro 距离 将是最常见的解决方案。我不确定 SQL Server 为此内置了什么(如果有的话)。如果不出意外,我认为您可以使用 SimMetrics 库,如此处。无论如何,它看起来就像这样。
You could, but you'd need to use another function to do it. Levenshtein ratio or Jaro distance would be the most common solutions. I'm not sure what, if anything, SQL Server includes builtin for this. If nothing else I think you can use the SimMetrics library as described here. Regardless, it would look something like this.
编辑
由于评论中的一些持续的刺激,我在这里展示了 SQL 中 Levenshtein 距离计算的实现。这里使用 SQL Server 2005+ 的 TSQL,但该技术也可以转换到其他 DBMS。 最高得分为 100。
注意 - 使用此行
CROSS JOIN (SELECT 'Jon' as LookFor) LookFor
是为了使输入'Jon'
不需要在查询中重复多次。还可以定义一个变量,并在查询中使用LookFor
的地方使用它。输出
值得注意的是,与 SOUNDEX 一起使用时,Jonny 的得分高于 Bone,而单独使用
Levenshtein
则不会发生这种情况。原始答案如下,基于 LIKE '%x%' 对输入进行预过滤,将 Levenshtein 折叠为简单的 Len(column) - Len(Like-expression) 计算
看一下这个示例 - 它测试长度和SOUNDEX 差异,因缺乏更好的措施。
最高得分为 100。
输出
EDIT
Due to some persistent prodding from the comments, I present here an implementation of the Levenshtein distance calculation in SQL. TSQL for SQL Server 2005+ is used here, but the technique can be converted to other DBMS as well. Maximum score is 100.
Note - This line
CROSS JOIN (SELECT 'Jon' as LookFor) LookFor
is used so that the input'Jon'
does not need to be repeated many times in the query. One could also define a variable instead and use it whereLookFor
is used in the query.Output
It is worth noting that together with SOUNDEX, Jonny gets to score higher than Bone which won't happen with
Levenshtein
alone.Original answer follows, based on pre-filtering the input based on LIKE '%x%' which collapses the Levenshtein to a simple Len(column) - Len(Like-expression) calculation
Have a look at this example - it tests the length and SOUNDEX differences, for lack of better measures.
The maximum score is 100.
Output
像这样的东西可能会有所帮助:
http://www.mombu.com/microsoft/microsoft/t-equivalent-sql-server-functions-for-match-against-in-mysql-2292412.html
Something like this might help:
http://www.mombu.com/microsoft/microsoft/t-equivalent-sql-server-functions-for-match-against-in-mysql-2292412.html