2016-04-19 65 views
-2

我有一個Mysql查詢,我試圖總結一列,並按總數排名。在mysql中的排名總和

這裏是我的查詢:

select 
@rownum:[email protected]+1 as rank, 
sum(length) as total, 
user_id 
from submissions, 
(select @rownum:=0) a WHERE id = 1067 AND status = 1 
group by user_id 
order by total desc 

這導致:

rank total user_id  
2  65.25  1360 
1  59.50  1151 
4  58.00  1250 
6  55.75  1374 
5  51.25  1154 
3  34.75  841 

回答

0

我認爲你必須使用一個內部查詢先搶總計然後用outter排名他們選擇這樣的:

select @rownum:[email protected]+1 as rank, 
     total, 
     user_id 
from 
    (select sum(length) as total, 
      user_id 
    from submissions 
    WHERE id = 1067 AND status = 1 
    group by user_id 
    order by total desc)T,(select @rownum:=0)a 
+0

是的,這是完美的作品。謝謝! –