Oracle有2个函数- rank()和density_rank()-我发现它们对某些应用程序非常有用。我现在在mysql中做某事,想知道他们是否有与之等效的东西?
没有直接等效的方法,但是您可以使用一些(效率不高)自联接来伪造它。来自MySQL查询howtos集合的一些示例代码:
SELECT v1.name, v1.votes, COUNT(v2.votes) AS Rank FROM votes v1 JOIN votes v2 ON v1.votes < v2.votes OR (v1.votes=v2.votes and v1.name = v2.name) GROUP BY v1.name, v1.votes ORDER BY v1.votes DESC, v1.name DESC; +-------+-------+------+ | name | votes | Rank | +-------+-------+------+ | Green | 50 | 1 | | Black | 40 | 2 | | White | 20 | 3 | | Brown | 20 | 3 | | Jones | 15 | 5 | | Smith | 10 | 6 | +-------+-------+------+