2016-10-03 85 views
2

我已經繼承了使用PHP腳本訪問SQL Server 2012數據庫的項目。該應用程序允許用戶輸入幾個參數並根據這些參數提出請求。按照字母數字字符串的相似性排序MS SQL Server 2012

我遇到問題的部分是允許用戶使用以字母數字字符串開頭的SKU搜索項目的字段。

這是已使用的項目查詢:

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%'); 

我注意到,該查詢返回正確的信息,而不是在一個有用的順序。

我不知道如何對它排序,以便SKU最接近匹配$ localSKU變量的行排在第一位。

我嘗試這一點,但它沒有我希望的效果:

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

我也做模糊匹配的字符串一些閱讀,但我不知道如何在這裏實現它。

是否有一個有效的辦法:

  1. 搜索開始給定的字符串基於該字符串的親近
  2. 秩序的結果,然後由值

預期結果爲varchar :

參數:

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

預期結果:

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

實際結果:

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

能否請您展示預期的結果和實際結果 – TheGameiswar

+0

另外,您能提供一些'$ localSKU'的例子嗎? – scsimon

+0

@TheGameiswar我爲你添加了那些,讓我知道如果我可以進一步澄清 –

回答

1

嘗試此查詢:

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; 

我希望它能幫助。

+0

這給我一個很好的結果,稍作調整: ORDER BY MyOrder ASC,IB.LocalSKU ASC; –

+0

我很高興它工作:) –

相關問題