因此,我尝试创建一个MySQL查询以获取RANK ID的平均值,并且该值必须介于8到12之间,否则查询将选择5个随机播放器名称,直到RANKVALUE的平均值介于8到12之间。

select summonername
from (select rankvalue from
players group by summonername
having count(rankvalue) = 5
order by rand())
group by summonername
having avg(rankvalue) between 8 and 12




tid  PLAYER NAME   RANKVALUE
1   summonername2   5
1   Summoner 1      22
1   Summoner 2      10
1   Summoner 3      14
1   Summoner 4      10
1   Summoner 2      21
1   test            21
1   yop             13
1   summonername6   12
1   summonername7   16
1   summonername8   5
1   summonername9   13
1   summonername10  21
1   summonername11  1
1   summonername12  9

最佳答案

使用Group byHaving子句

SELECT `summonername`, AVG(rankvalue)
FROM `players`
Where tid=1
group by summonername
Having AVG(rankvalue) between 8 and 12
ORDER BY rand() LIMIT 5

08-06 03:05