我执行此类查询时返回表中的重复行, 这有什么问题?????
Declare @Keyword AS NVARCHAR(MAX)
Set @Keyword = 'scale'
select DISTINCT * from (
SELECT DISTINCT dbo.Product_Search.Product_Name, dbo.Product_Search.Product_Code,
dbo.Product_Search.Product_ModelNo, dbo.Brand_Master.Brand_Name,
dbo.Product_Search.Product_Kid, dbo.Product_Search.Product_Image,
dbo.Unit_Master.Unit_Name, dbo.product_Search.CatName,
dbo.product_Search.ProdName, dbo.product_Search.ProductDescription, ROW_NUMBER() over(order by Product_Kid) as ROW
FROM dbo.Product_Search INNER JOIN
dbo.Brand_Master ON dbo.Product_Search.Product_BrandId = dbo.Brand_Master.Brand_Kid LEFT OUTER JOIN
dbo.Category_Index_Master ON dbo.Product_Search.Product_CategoryId = dbo.Category_Index_Master.Category_Kid LEFT OUTER JOIN
dbo.Unit_Master ON dbo.Product_Search.Product_MarketPriceType = dbo.Unit_Master.Unit_Kid
WHERE (dbo.Product_Search.Product_Name LIKE '%' + @Keyword + '%')
OR CategoryIndex_Name LIKE '%' + @Keyword + '%'
OR Brand_Name LIKE '%' + @Keyword + '%'
OR Product_ModelNo LIKE '%' + @Keyword + '%'
OR Product_Code LIKE '%' + @Keyword + '%'
)h
答案 0 :(得分:1)
ROW_NUMBER: http://msdn.microsoft.com/en-us/library/ms186734.aspx
返回结果分区中行的序号 设置,从1开始,每个分区的第一行
因此,您将始终拥有非唯一值。让你试着将ROW向上移动:
SELECT *,ROW_NUMBER() OVER ( ORDER BY Product_Kid ) AS ROW
FROM ( SELECT DISTINCT
dbo.Product_Search.Product_Name ,
dbo.Product_Search.Product_Code ,
dbo.Product_Search.Product_ModelNo ,
dbo.Brand_Master.Brand_Name ,
dbo.Product_Search.Product_Kid ,
dbo.Product_Search.Product_Image ,
dbo.Unit_Master.Unit_Name ,
dbo.product_Search.CatName ,
dbo.product_Search.ProdName ,
dbo.product_Search.ProductDescription
FROM dbo.Product_Search
INNER JOIN dbo.Brand_Master ON dbo.Product_Search.Product_BrandId = dbo.Brand_Master.Brand_Kid
LEFT OUTER JOIN dbo.Category_Index_Master ON dbo.Product_Search.Product_CategoryId = dbo.Category_Index_Master.Category_Kid
LEFT OUTER JOIN dbo.Unit_Master ON dbo.Product_Search.Product_MarketPriceType = dbo.Unit_Master.Unit_Kid
WHERE ( dbo.Product_Search.Product_Name LIKE '%' + @Keyword
+ '%' )
OR CategoryIndex_Name LIKE '%' + @Keyword + '%'
OR Brand_Name LIKE '%' + @Keyword + '%'
OR Product_ModelNo LIKE '%' + @Keyword + '%'
OR Product_Code LIKE '%' + @Keyword + '%'
) h