一尘不染

Mysql中的MSSQL Row_Number()over(order by)

mysql

我需要使用以下查询创建group by子句:

SELECT NAME, SUM(donationvalue) FROM tbl_pdm2k10_Donations
GROUP BY NAME
ORDER BY SUM(donationvalue) desc

但我想在左边的一栏返回我:1、2、3、4、5等。在MSSql中我会做

  SELECT Row_Number() Over(order by SUM(donationvalue) desc), NAME, SUM(donationvalue)      
 FROM tbl_pdm2k10_Donations
    GROUP BY NAME

这可能吗?


阅读 530

收藏
2020-05-17

共1个答案

一尘不染

set @row:=0;
SELECT 
  @row:=@row+1 as row_numer,
  NAME, 
  SUM(donationvalue) as total 
FROM tbl_pdm2k10_Donations
GROUP BY NAME
ORDER BY total desc
2020-05-17