Categories
Coding

Row numbers within MySQL

Sometime you will need a query that will return a resultset of ranked records. The following MySQL query will do the trick: SELECT @rownum:=@rownum+1 AS rank, companies.* FROM conpanies p, (SELECT @rownum:=0) r ORDER BY profit DESC LIMIT 20;

Sometime you will need a query that will return a resultset of ranked records. The following MySQL query will do the trick:

SELECT @rownum:=@rownum+1 AS rank, companies.* FROM conpanies p, (SELECT @rownum:=0) r ORDER BY profit DESC LIMIT 20;