我有一个带有id(商店用户ID)的表,并且在不同的匹配中得分。我想要用户的位置。
所以我试试这个sql fiddle; 在这个我得到了所有的行,但我只需要用户ID为3并且它在表中的位置。
像这样:Score Postion
26 3
即使我尝试这样做但没有成功
答案 0 :(得分:2)
我得到了答案:http://sqlfiddle.com/#!2/b787a/2
select * from (
select T.*,(@rownum := @rownum + 1) as rownum from (
select sum(score) as S,id from mytable group by id order by S desc ) as T
JOIN (SELECT @rownum := 0) r
) as w where id = 3
更新了sqlfiddle及以上查询。现在它工作得很好。
答案 1 :(得分:0)
只需添加一个where子句
select x.id,x.sum,x.rownum
from(
select id,sum(score) as sum,(@rownum := @rownum + 1) as rownum
from mytable
JOIN (SELECT @rownum := 0) r
group by id
) x
where id =3
答案 2 :(得分:0)
我认为这应该可以解决问题:
SELECT totalScore, rownum FROM (
SELECT id,sum(score) AS totalScore,(@rownum := @rownum + 1) AS rownum
FROM mytable
JOIN (SELECT @rownum := 0) r
group by id) result
WHERE result.ID = 3;