Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
mysql中的排序和_Mysql_Rank - Fatal编程技术网

mysql中的排序和

mysql中的排序和,mysql,rank,Mysql,Rank,我有一个Mysql查询,我试图对一列求和,并按总数排序 我的问题是: select @rownum:=@rownum+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

我有一个Mysql查询,我试图对一列求和,并按总数排序

我的问题是:

select 
@rownum:=@rownum+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

我认为您必须先使用内部查询获取总数,然后使用outter select对其进行排序,如下所示:

select @rownum:=@rownum+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