以下查询根据用户投票的次数对投票进行排序...我想知道特定用户队列中的#。
SELECT @s:=@s+1 serial_number, user_id, COUNT(slug_owner) as cnt
FROM `votes_queue`,(SELECT @s:= 0) AS s
GROUP BY slug_owner
ORDER BY cnt DESC
serial_number | user_id | cnt
3 | 19 | 8
2 | 14 | 4
1 | 13 | 2
基本上我需要反转serial_number列中的数字,这样我就可以根据投票判断用户13是#3了。
答案 0 :(得分:0)
在生成有序计数后分配序列号:
SELECT @s:=@s+1 serial_number, temp.*
FROM (
SELECT user_id, COUNT(slug_owner) as cnt
FROM `votes_queue`,
GROUP BY slug_owner
ORDER BY cnt DESC
) temp, (SELECT @s:= 0) AS s
答案 1 :(得分:0)
序列号不会反转,因为GROUP BY
和变量不会混合。 ORDER BY
很好。您可以使用@ hjpotter92的解决方案,但以下内容也解决了问题:
SELECT (@rn := @rn + 1) as serial_number, t.*
FROM (SELECT serial_number, user_id, COUNT(slug_owner) as cnt
FROM `votes_queue`
GROUP BY slug_owner
) t CROSS JOIN
(SELECT @rn := 0) params
ORDER BY cnt DESC;
我认为表现是一样的。我提供这个答案只是为了澄清实际问题是什么。