根据搜索结果过滤特定列

时间:2017-02-28 15:42:15

标签: sql-server sql-like

我有一个搜索栏和一个从我的sql server数据库填充的HTML表。每当我搜索时,它都会在LIKESKU上使用SKU Description命令来过滤表格。显示结果时,我希望它根据与SKU列最匹配的内容过滤结果。

例如,如果我搜索"100",则会显示位于SKUSKU Description列中的结果。但是,我希望它进行过滤,以便过滤SKU列中匹配的值,使其在SKU Description列中匹配的值之前首先显示。

我认为这只是在我的查询中添加内容的问题,但我该如何做?

以下是我的问题:

$query = "SELECT Product_ID, [Major Category], [Minor Category], [Product Report Code], SKU, [SKU Description], [SKU Status], CAST([Create Date] AS DATE) AS Date, Group_ID, [SKU Group]
          FROM vProducts
          WHERE CONCAT(SKU, [SKU Description])
          LIKE '%".$valueToSearch."%'";

1 个答案:

答案 0 :(得分:1)

这可能有点矫枉过正,您必须将其格式化为“$ query”。这是sql。

SELECT A.Product_ID, A.[Major Category], A.[Minor Category], A.[Product Report Code], A.SKU, A.[SKU Description], A.[SKU Status], A.Date, A.Group_ID, A.[SKU Group], A.Sort
FROM (
        SELECT Product_ID, [Major Category], [Minor Category], [Product Report Code], SKU, [SKU Description], [SKU Status], CAST([Create Date] AS DATE) AS Date, Group_ID, [SKU Group], 1 AS Sort
        FROM vProducts
        WHERE SKU LIKE '%' + @value + '%'

        UNION all

        SELECT Product_ID, [Major Category], [Minor Category], [Product Report Code], SKU, [SKU Description], [SKU Status], CAST([Create Date] AS DATE) AS Date, Group_ID, [SKU Group], 2 AS Sort
        FROM vProducts
        WHERE [SKU Description] LIKE '%' + @value + '%'
    ) A
ORDER BY A.Sort