简体   繁体   English

按字母数字字符串 MS SQL Server 2012 中的相似性排序

[英]Sorting by similarity in alphanumeric string MS SQL Server 2012

I've inherited a project that is using a PHP script to access a SQL Server 2012 database.我继承了一个使用 PHP 脚本访问 SQL Server 2012 数据库的项目。 The application allows a user to input several parameters and make a request based on those parameters.该应用程序允许用户输入多个参数并根据这些参数发出请求。

The piece I'm having problems with is a field that allows users to search for an item with a SKU beginning with a string of alphanumeric characters.我遇到的问题是一个字段,该字段允许用户搜索 SKU 以一串字母数字字符开头的项目。

This is the query that was used by the project:这是项目使用的查询:

SELECT top 10 IB.LocalSKU
, Cast(Round(IB.GoalMargin, 2) AS Decimal(10,2)) AS GoalMargin
, CASE WHEN IB.MAP = 0 THEN NULL ELSE IB.MAP END AS Min
, IB.ProductCost
, IB.ShippingEstimate
, EB.Price AS CurrentPrice


FROM intra.InventoryBase IB INNER JOIN intra.DropshipChannelAdvisorSKUs CA
  ON IB.LocalSKU = CA.LocalSKU
LEFT JOIN intra.eBayQoHFeedback EB
  ON CA.ChannelAdvisorSKU = EB.SKU

WHERE LOWER(IB.LocalSKU) LIKE LOWER('$localSKU%')
  AND LOWER(SupplierID) LIKE LOWER('%$supplierId%')
  AND LOWER(IB.Category) LIKE LOWER('%$category%')
  AND LOWER(IB.Dropship) LIKE LOWER('%$dropship%');

I have noticed that the query returns the correct information, but not in a useful order.我注意到查询返回了正确的信息,但没有按照有用的顺序。

I am not sure how to sort it so that the rows with SKUs most closely matching the $localSKU variable are ranked first.我不确定如何对其进行排序,以便将 SKU 与 $localSKU 变量最匹配的行排在第一位。

I attempted this, but it didn't have the effect I hoped for:我尝试了这个,但没有达到我希望的效果:

ORDER BY Difference(IB.LocalSKU, '$localSKU%') ASC

I've also done some reading on Fuzzy String matching, but I'm not sure how to implement it here.我还阅读了一些关于模糊字符串匹配的文章,但我不确定如何在这里实现它。

Is there an effective way to:有没有有效的方法:

  1. Search for a varchar starting with a given string搜索以给定字符串开头的 varchar
  2. Order results based on the closeness of that string, then by value根据该字符串的接近程度对结果排序,然后按值排序

Expected Result:预期结果:

Parameters:参数:

$localSKU = "FMCPL1CY00"; 
$supplierId = 87; 
$category = "Premium Floor Liners"; 
$dropship = True;

Expected results:预期成绩:

--------------------------------------------------------------------------------
| FMCPL1CY00* | GoalMargin | 0 | ProductCost | ShippingEstimate | CurrentPrice |
| FMCPL1CY01* | GoalMargin | 0 | ProductCost | ShippingEstimate | CurrentPrice |
| FMCPL1CY02* | GoalMargin | 0 | ProductCost | ShippingEstimate | CurrentPrice |
| FMCPL1CY03* | GoalMargin | 0 | ProductCost | ShippingEstimate | CurrentPrice |
--------------------------------------------------------------------------------

Actual Results:实际结果:

-------------------------------------------------------------------------------------
| FMCPL1CH04221509 | GoalMargin | 0 | ProductCost | ShippingEstimate | CurrentPrice |
| FMCPM1SA0021302  | GoalMargin | 0 | ProductCost | ShippingEstimate | CurrentPrice |
| FMCPL1TY07801509 | GoalMargin | 0 | ProductCost | ShippingEstimate | CurrentPrice |
| FMCPL1TY05721502 | GoalMargin | 0 | ProductCost | ShippingEstimate | CurrentPrice |
-------------------------------------------------------------------------------------

Try this query:试试这个查询:

SELECT top 10 IB.LocalSKU
, Cast(Round(IB.GoalMargin, 2) AS Decimal(10,2)) AS GoalMargin
, CASE WHEN IB.MAP = 0 THEN NULL ELSE IB.MAP END AS Min
, IB.ProductCost
, IB.ShippingEstimate
, EB.Price AS CurrentPrice

,CASE WHEN IB.LocalSKU LIKE '$localSKU%' THEN 0  else 1 END as MyOrder

FROM intra.InventoryBase IB INNER JOIN intra.DropshipChannelAdvisorSKUs CA
  ON IB.LocalSKU = CA.LocalSKU
LEFT JOIN intra.eBayQoHFeedback EB
  ON CA.ChannelAdvisorSKU = EB.SKU

WHERE LOWER(IB.LocalSKU) LIKE LOWER('$localSKU%')
  AND LOWER(SupplierID) LIKE LOWER('%$supplierId%')
  AND LOWER(IB.Category) LIKE LOWER('%$category%')
  AND LOWER(IB.Dropship) LIKE LOWER('%$dropship%')

ORDER BY MyOrder ASC;

I hope it helps.我希望它有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM