我正在尝试创建一个高尔夫实时排行榜,该排行榜基于玩家拥有的分数等于理想分数。
理想点是每个孔2个点。因此,如果一个玩家打了3个洞,他应该得到6分。如果玩家有6点,则玩家点数将为0。如果玩家赢得了更多积分,则他将获得+点数和更少点数。
这里的问题是,玩家将在不同的洞中,有些人打了2,有些人打了3,也许有些人打了7。减去。
这是我到目前为止所得到的:
$result = mysqli_query($con,"SELECT SUM(ls.points) points
, COUNT(ls.fk_playerid) holes
, ls.fk_playerid
, u.fname
, u.lname
FROM ".$prefix."_livescore ls
INNER JOIN ".$prefix."_users u
ON ls.fk_playerid = u.userid
WHERE fk_gameid=$gameid GROUP BY fk_playerid ORDER BY points DESC");
while($row = mysqli_fetch_array($result)){
$idealpoints = $row['points'] - ($row['holes'] * 2);
$players[] = array('uid' => $row['fk_playerid'], 'name' => $row['fname'].' '.$row['lname'], 'hole' => $row['holes'], 'points' => $idealpoints);
}
在这个例子中,我只是计算点数和排序。然后在循环中,我找出理想点。这使得排行榜不正确,因为在最有可能打洞最多的玩家将处于领先地位...
希望这有道理并希望获得帮助... :-)
最佳答案
SELECT fk_playerid, fname, lname, points, holes, points-2*holes diff
FROM (
SELECT SUM(ls.points) points
COUNT(ls.dk_playerid) holes,
ls.fk_playerid, u.fname, u.lname
FROM {$prefix}_livescore ls
INNER JOIN {$prefix}_users u
ON ls.fk_playerid = u.userid
WHERE fk_gameid = $gameid
GROUP BY fk_playerid) x
ORDER BY diff desc
关于php - 将数学放入sql查询,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17771267/