有没有办法找到查询的价值?

时间:2015-04-06 00:02:20

标签: php mysql

我最近制作了一个系统,根据他们的分数对每个玩家进行排名。那么系统获得积分的方式相当混乱。使用这个系统超过24小时后,我发现它没有根据要点组织它。但后来我突然想到,我可能以不代表SQL查询的方式计算错误点。这是我的排名使用的SQL查询:

SELECT * , playeruid AS player_id, (
(
    SELECT COALESCE(sum(player1points),0) 
    FROM  `mybb_matches` 
    WHERE player1uid = player_id AND gid = $id AND timestamp < $time AND winneruid is NOT NULL AND dispute != 3 )
+
(
    SELECT COALESCE(sum(player2points),0) 
    FROM  `mybb_matches` 
    WHERE player2uid = player_id AND gid = $id AND timestamp < $time AND winneruid is NOT NULL AND dispute != 3 )
+
(
    SELECT SUM( rank ) 
    FROM  `mybb_matchesgame` 
    WHERE playeruid = player_id AND gid = $id )
)

 AS points
 FROM mybb_matchesgame WHERE gid = $id
 ORDER BY points DESC

现在显示了这一点,我想知道是否有任何方法可以获得&#34;积分&#34;并以某种方式显示它,以便我可以验证数字。这可能吗?

1 个答案:

答案 0 :(得分:0)

查询中没有group by语句,因此SUM很可能不会超出预期的集合。 COALESCE也可以用IFNULL代替,这可能会更有效率。

SELECT q.* , playeruid AS player_id, a.points+b.points+c.points AS points
FROM mybb_matchesgame q
LEFT JOIN (
    SELECT IFNULL(SUM(player1points),0) as points,player_id
    FROM  `mybb_matches` 
    WHERE timestamp < $time AND winneruid is NOT NULL AND dispute != 3 
 GROUP BY player_id) a ON player1uid = a.player_id
LEFT JOIN (
    SELECT IFNULL(sum(player2points),0) as points,player_id
    FROM  `mybb_matches` 
    WHERE timestamp < $time AND winneruid is NOT NULL AND dispute != 3 
GROUP BY player_id) b ON player2uid = b.player_id 
LEFT JOIN (
    SELECT IFNULL(SUM( rank ),0) as points,player_id
    FROM  `mybb_matchesgame` 
GROUP BY player_id) c ON playeruid = c.player_id 
  WHERE gid = $id
 ORDER BY a.points+b.points+c.points DESC;