我有一个表some_magic_function
,其中jackpot
列代表用户ID,uid
代表正确答案数量。
我设法通过正确的答案对用户进行选择和排名,但下一步是什么?
nright
我需要根据他的位置计算给每个用户的分数。
只有前三名MAX SELECT
a1.uid,
a1.nright,
COUNT(a2.nright) AS rank
FROM
jackpot a1,
jackpot a2
WHERE
a1.nright < a2.nright
OR (
a1.nright = a2.nright
AND a1.uid = a2.uid
)
GROUP BY
a1.uid,
a1.nright
ORDER BY
a1.nright DESC,
a1.uid DESC
的用户才能获得积分。
总积分数=用户数* 20。
第一名占总数的70%,第二名占20%,第三名占10%。
如果用户之间的答案权利相同,则均分(50 / 50,33 / 33/33 ......)。
答案 0 :(得分:2)
你需要分解你想要的东西。
第一步:您想获得前3名。
SELECT nright
FROM jackpot
ORDER BY nright DESC
LIMIT 3
第二步:获得此前3分的用户ID
SELECT j.uid
FROM jackpot j
INNER JOIN (
SELECT nright
FROM jackpot
ORDER BY nright DESC
LIMIT 3 ) AS t ON t.nright = j.nright
第3步:总点数
SELECT COUNT(uid)*20 AS lot FROM jackpot
第4步:排名和人数
这里我们需要使用一个变量,就像在php中一样,你不能使用set @var:= X;
,所以诀窍是做一个Select @var:= X
,这个变量不会起作用,因为聚合函数。所以你需要这样做:
SELECT @rank := @rank+1 as rank,T1.nright,T1.nb,T1.lot
FROM(
SELECT nright,
COUNT(uid) as nb,
(SELECT COUNT(uid)*20 FROM jackpot) as lot
FROM jackpot
GROUP BY nright
ORDER BY nright DESC
LIMIT 3
)T1, (SELECT @rank:= 0) r
第5步:批量分配
SELECT j.uid,
CASE
WHEN t.rank = 1 THEN (t.lot*0.7)/t.nb
WHEN t.rank = 2 THEN (t.lot*0.2)/t.nb
WHEN t.rank = 3 THEN (t.lot*0.1)/t.nb
END as lot
FROM jackpot j
INNER JOIN
(SELECT @rank := @rank+1 as rank,T1.nright,T1.nb,T1.lot
FROM(
SELECT nright,
COUNT(uid) as nb,
(SELECT COUNT(uid)*20 FROM jackpot) as lot
FROM jackpot
GROUP BY nright
ORDER BY nright DESC
LIMIT 3
)T1, (SELECT @rank:= 0) r) t ON t.nright = j.nright