6

我有一个 MySQL 表,其中有一个数字列表,这些数字来自一个游戏,用户必须单击一个按钮才能接近 100。

我以为我会使用AVG(),但这显然行不通,因为当您有 0 和 200 时,看起来好像您获得了完美的 100。

我需要的是每一轮(有 10 个)计算这个数字离 100 有多远,然后数一下。

Example:
User A: 98 + 102 + 102 = 6 (something) Loser!
User B: 95 + 100 + 100 = 5 (something) Winner!

我有什么,但显然有缺陷:

SELECT user_id, AVG(score) AS Average, (100-AVG(score)) AS Difference, 
DATE(playtime) AS Playdate 
FROM playtable
GROUP BY user_id, DATE(playtime) 
ORDER BY Playdate DESC, user_id
4

2 回答 2

7

这应该这样做

SELECT user_id, SUM( ABS(100-score) ) as cumulative_error,
  DATE(playtime) AS Playdate 
FROM playtable
GROUP BY user_id, DATE(playtime) 
ORDER BY Playdate DESC, user_id

累积误差将包含累积的差值

于 2012-12-19T23:21:36.750 回答
1

您可以修复您的查询以返回绝对值,以查看分数与给定值的距离(在您的情况下为 100)

SELECT user_id, AVG(score) AS Average, SUM(ABS(100 - score)) AS Difference, 
DATE(playtime) AS Playdate 
FROM playtable
GROUP BY user_id, DATE(playtime) 
ORDER BY Playdate DESC, user_id
于 2012-12-19T23:20:48.787 回答