你需要分解你想要什麼。
第一步:你想得到前3的分數。
SELECT nright
FROM jackpot
ORDER BY nright DESC
LIMIT 3
第2步:用戶ID誰得到這3個先得
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
第三步:點總量
SELECT COUNT(uid)*20 AS lot FROM jackpot
第四步:等級和人數
這裏我們需要使用一個變量,就像你在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
如果您發佈的SQL的DML和DDL和如果你做的這將是非常非常有幫助[SQL小提琴](http://sqlfiddle.com) –