Mysql – Rank using one column. If it is the same value in 2 rows rank using another column

MySQLrank

i have a table as table1

 id| hits | time
-------+--------+------
 1 | 9    |    40.89
 2 | 5    |    33.21
 3 | 5    |    78.31
 4 | 1    |    71.93
 5 | 4    |    23.10
 6 | 4    |    99.99
 7 | 8    |    12.32
 8 | 7    |    32.64


I want to rank the values using hits and if 2 rows has the same number of hits i want to rank them using time
That is one with lowest time has a higher rank
This is the expected result


  id| hits | time     | rank 
-------+--------+--------------
 1 | 9    |    40.89  |  1
 2 | 5    |    33.21  |  4
 3 | 5    |    78.31  |  5
 4 | 1    |    71.93  |  8
 5 | 4    |    23.10  |  6
 6 | 4    |    99.99  |  7
 7 | 8    |    12.32  |  2
 8 | 7    |    32.64  |  3

Any help is appreciated

Best Answer

SELECT *, RANK() OVER (ORDER BY hits DESC, `time` ASC) AS `rank`
FROM table1
ORDER BY id