For example I have this query which orders val2 according to their item with max count.
INSERT INTO table d (val1, val2 ,cnt)
SELECT val1, val2 , max(cnt) as cnt
FROM table2 WHERE val1 =x GROUP BY
val2 ORDER BY max(cnt) DESC LIMIT 10;
Instead I want to rank the results so cnt isnt going to be a random number but 1 if its respective value in table 2 has the highest count, 2 if its the 2nd highest etc.
I have to agree with hobodave that it would probably be better to rank as you fetch instead of as you insert, so you should reconsider if you really want to do this. But if this is actually what you need, it can be done as you requested like this:
Result:
Test data: