Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.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_List_Ranking - Fatal编程技术网

mysql获取订单号

mysql获取订单号,mysql,list,ranking,Mysql,List,Ranking,我有以下结构 国家/地区-用户ID-积分 8402324 840 32 31 840 22 38 840 15 35 8401020 250 15 33 7241712 等 我想获得用户在每个国家排名中的位置,以分数为单位 我正在使用 select @rownum:=@rownum+1 Num, Country, UserId, points from users , (SELECT @rownum:=0) r where country=840 order by points DESC

我有以下结构


国家/地区-用户ID-积分

8402324
840 32 31
840 22 38
840 15 35
8401020
250 15 33
7241712

我想获得用户在每个国家排名中的位置,以分数为单位

我正在使用

select @rownum:=@rownum+1 Num, Country, UserId, points 
from users , (SELECT @rownum:=0) r  
where country=840 order by points DESC ;
我想得到一个用户在他的国家的位置

在本例中,在国家840,如果我选择用户id=23,我将获得位置4

国家/地区-用户ID-积分-订单
84022381
84015352
84032313
84023244

84010 20 5

使用您的查询,您将在结果中收到行号,因此它不是您想要的。最好的方法是生成位置并将其保存到分隔列中。这样,您就可以轻松地选择它,而无需每次都重新计算它(这一点非常重要)

要执行此操作,您可以修改查询以更新行,而不是选择行。

尝试执行以下操作:

select * from (
  select @rownum: = @rownum + 1 Num,
        Country,
        UserId,
        points
  from users, (select @rownum: = 0) r
  where country = 840
  order by points desc
) a
where userId = 23

它起作用了。非常感谢菲利佩