先求出总和 $query = mysql_query("select uid,sum() as from table group by uid order by desc"); $mc = 0; while($res = mysql_fetch_row($query)) { if($res['uid'] != 4) $mc++; else break; }echo $mc;
没想出很好的方法 select uid, sum() as 1 from table group by uid order by 1 ASC; 再用循环找出uid=4的位置
推荐你建立另外一个表:uid total 就是编号和总分。
多个insert|update而已,除了多占点空间以外。
create table tb ( uid int, int ) insert into tb select 1,9 union all select 2,5 union all select 3,8 union all select 4,6 select uid,mc=(select count(1) from tb where >(select from tb where uid=4)) from tb where uid=4 /* uid mc ----------- ----------- 4 2(所影响的行数为 1 行) */
我以前是这么做的
mysql还没这么智能。简单的说:你可以认为uid的名次是2,也可以认为是3
正常数据应该是
uid
1 7
1 6
2 3
2 2
2 5
3 4
3 3
4 8
4 1
4 3
我就是想查uid=4的总分的排名
$query = mysql_query("select uid,sum() as from table group by uid order by desc");
$mc = 0;
while($res = mysql_fetch_row($query))
{
if($res['uid'] != 4) $mc++;
else break;
}echo $mc;
select uid, sum() as 1 from table group by uid order by 1 ASC;
再用循环找出uid=4的位置
就是编号和总分。
(
uid int,
int
)
insert into tb select 1,9
union all select 2,5
union all select 3,8
union all select 4,6
select uid,mc=(select count(1) from tb where >(select from tb where uid=4))
from tb
where uid=4
/*
uid mc
----------- -----------
4 2(所影响的行数为 1 行)
*/