select a.*, rank() over (partition by a.field1 order by a.field2 desc) field_rank
from table_a a
order by a.field1, a.field2
被翻译成MySQL?
This question似乎相似,但在基本查询的末尾没有Order By。此外,它是否由分区字段排序是否重要?
1 个答案:
答案 0 :(得分:16)
根据您提供的链接,它应如下所示:
SELECT a.*,
(
CASE a.field1
WHEN @curType
THEN @curRow := @curRow + 1
ELSE @curRow := 1 AND @curType := a.field1 END
) + 1 AS rank
FROM table_a a,
(SELECT @curRow := 0, @curType := '') r
ORDER BY a.field1, a.field2 desc;