相当于MySQL中的ROW_NUMBER()

时间:2017-03-08 17:20:12

标签: mysql row-number

我正在尝试将以下SQL查询转换为MySQL,但想知道MySQL中是否存在等效的ROW_NUMBER()。

 SELECT TOP (@NumResults) * FROM (   
    SELECT 
      ROW_NUMBER() OVER (
        ORDER BY 
          CASE WHEN @OrderBy = 'PRODUCT' AND @OrderByDirection = 'D' THEN product_name END DESC,
          CASE WHEN @OrderBy = 'PRODUCT' AND @OrderByDirection = 'A' THEN product_name END,
          CASE WHEN @OrderBy = 'CATEGORY' AND @OrderByDirection = 'D' THEN category_name END DESC,
          CASE WHEN @OrderBy = 'CATEGORY' AND @OrderByDirection = 'A' THEN category_name END,
          CASE WHEN @OrderBy = 'MANUFACTURER' AND @OrderByDirection = 'D' THEN manufacturer_name END DESC,
          CASE WHEN @OrderBy = 'MANUFACTURER' AND @OrderByDirection = 'A' THEN manufacturer_name END, product_name 
      )AS RowNumber, *
    FROM #TempDataFeedTable
    WHERE manufacturer_name like @ManufacturerName
    ) _temp 
  WHERE RowNumber > (@Index - 1)

我应该使用自我加入吗?我已经谷歌搜索并查看堆栈溢出中的所有其他解决方案,但由于我的SQL查询有多个select语句,我无法获得正确的结果。有人可以帮忙吗?

这是我到目前为止所尝试过的,但是我得到的结果与SQL数据库的结果不一样。

SET @row_number:=0;
SELECT * FROM ( 
  SELECT   (@row_number:=@row_number + 1) As RowNumber, importeddatafeedid, product_name, categoryid,manufacturerid, image_url, link_url, upc, mpn, description, msrp, manufacturer_name FROM    
TempDataFeedTable 
    WHERE manufacturer_name like p_ManufacturerName 
        ORDER BY 
          CASE WHEN p_OrderBy = 'PRODUCT' AND p_OrderByDirection = 'D' THEN product_name END DESC,
          CASE WHEN p_OrderBy = 'PRODUCT' AND p_OrderByDirection = 'A' THEN product_name END,
          CASE WHEN p_OrderBy = 'CATEGORY' AND p_OrderByDirection = 'D' THEN category_name END DESC,
          CASE WHEN p_OrderBy = 'CATEGORY' AND p_OrderByDirection = 'A' THEN category_name END,
          CASE WHEN p_OrderBy = 'MANUFACTURER' AND p_OrderByDirection = 'D' THEN manufacturer_name END DESC,
          CASE WHEN p_OrderBy = 'MANUFACTURER' AND p_OrderByDirection = 'A' THEN manufacturer_name END, product_name 

)
_temp 
  WHERE RowNumber > (p_Index - 1)
 LIMIT 0,10;

0 个答案:

没有答案