MySQL Query Help Finding Rank by ID
I'm trying to modify the following query to find the rank of a specific videoid and I'm not having much luck can anyone suggest a solution?
SELECT videoid wins/loses as win_loss,
@curRank := @curRank + 1 AS rank
FROM cb_video,
(SELECT @curRank := 0) r
ORDER BY wins/loses DESC
I tried doing a subquery like th开发者_如何学Gois but it fails:
SELECT rank
FROM (SELECT videoid wins/loses as win_loss,
@curRank := @curRank + 1 AS rank
FROM cb_video,
(SELECT @curRank := 0) r
ORDER BY wins/loses DESC)
WHERE videoid = 116
Also adding the videoid to the WHERE clause without a subquery just always shows the rank being the #1 position as it only returns one row:
SELECT videoid wins/loses as win_loss,
@curRank := @curRank + 1 AS rank
FROM cb_video,
(SELECT @curRank := 0) r
WHERE videoid = 116
ORDER BY wins/loses DESC
Any ideas how to limit the result to a specific ID but still retain the rank? FYI I keep two columns (wins and loses) if that helps.
SELECT a.videoid,
(SELECT COUNT(*) FROM cb_video b
WHERE a.videoid !=b.videoid
AND (b.wins/b.loses) > (a.wins/a.loses))+1 AS rank
FROM cb_video a
WHERE a.videoid = 116
Try something like this:
SELECT videoid, rank FROM (SELECT videoid, wins/loses as win_loss, @curRank := @curRank + 1 AS rank FROM cb_video, (SELECT @curRank := 0) r ORDER BY wins/loses DESC) s WHERE videoid = 116
I've tested this on simple subset created of similar table as you've described... It returns the ONE video and its actual final Rank of the entire set...
select *
from ( SELECT
videoid,
wins,
losses,
wins/losses,
@curRank := @curRank +1 Rank
FROM
cb_video,
( select @curRank := 0 ) r
order by
wins/losses desc ) PreQuery
where
PreQuery.VideoID = 116
精彩评论