所以我有这张桌子;
的MySQL>描述player_weapon_stats;
+------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| players_id | int(10) unsigned | NO | | NULL | |
| weapons_id | int(10) unsigned | NO | | NULL | |
| matches_id | int(10) unsigned | NO | | NULL | |
| hits | int(10) unsigned | NO | | NULL | |
| shots | int(10) unsigned | NO | | NULL | |
| kills | int(10) unsigned | NO | | NULL | |
| acc | decimal(4,2) | NO | | NULL | |
+------------+------------------+------+-----+---------+----------------+
8 rows in set (0.00 sec)
有很多行(目前大约400k),像这样;
的MySQL> select * from player_weapon_stats ORDER BY id ASC LIMIT 5;
+----+------------+------------+------------+------+-------+-------+-------+
| id | players_id | weapons_id | matches_id | hits | shots | kills | acc |
+----+------------+------------+------------+------+-------+-------+-------+
| 1 | 1 | 1 | 1 | 5 | 0 | 1 | 0.00 |
| 2 | 1 | 2 | 1 | 133 | 437 | 2 | 30.43 |
| 3 | 1 | 3 | 1 | 247 | 896 | 8 | 27.57 |
| 4 | 1 | 4 | 1 | 0 | 11 | 0 | 0.00 |
| 5 | 1 | 5 | 1 | 35 | 59 | 9 | 59.32 |
+----+------------+------------+------------+------+-------+-------+-------+
5 rows in set (0.02 sec)
所以每场比赛每位玩家的武器统计数据会被记录
我想要做的是根据每个玩家的总和来获得每个weapon_id的最高acc
返回的行数应与武器数量相等(在本例中为8)
这是我试过的;
mysql> SELECT players_id, weapons_id, SUM(hits) AS hits, SUM(shots) AS shots, SUM(kills) AS kills, (FORMAT(hits / shots, 4) * 100) AS acc FROM player_weapon_stats GROUP BY weapons_id ORDER BY acc DESC;
// no player association so SUM totals up all players together
mysql> SELECT players_id, weapons_id, SUM(hits) AS hits, SUM(shots) AS shots, SUM(kills) AS kills, (FORMAT(hits / shots, 4) * 100) AS acc FROM player_weapon_stats GROUP BY weapons_id, players_id ORDER BY acc DESC, weapons_id ASC LIMIT 10;
// incorrect acc and around 25k rows returned
mysql> SELECT players_id, weapons_id, SUM(hits) AS hits, SUM(shots) AS shots, SUM(kills) AS kills, (FORMAT(hits / shots, 4) * 100) AS acc FROM player_weapon_stats GROUP BY players_id, weapons_id ORDER BY acc DESC, weapons_id ASC;
// appears correct acc, and correct totals but returns around 25k rows as well
我已经尝试了很多以上的变化以及当时想到的任何其他东西,但我仍然卡住了...我想我一直盯着它看太久了
任何人都可以帮助我吗?
----编辑
我使用的样本数据有点太小而无法编译成结果,因为每个players_id的每个weapon_id会有多个条目,然后将这些条目加在一起形成该玩家/武器的“平均/整体”; < / p>
当你看到4个玩家的每个武器的累加总数时......所以预期的结果将类似于那个,但每个武器只需一行
我不确定如何解释它
----第二次编辑
mysql> SELECT players_id, weapons_id, MAX(acc) FROM (SELECT weapons_id, players_id, AVG(acc) AS acc FROM player_weapon_stats GROUP BY players_id, weapons_id) AS t1 GROUP BY weapons_id;
+------------+------------+-----------+
| players_id | weapons_id | MAX(acc) |
+------------+------------+-----------+
| 1 | 0 | 25.000000 |
| 1 | 1 | 0.000000 |
| 1 | 2 | 84.995000 |
| 1 | 3 | 99.990000 |
| 1 | 4 | 99.990000 |
| 1 | 5 | 94.290000 |
| 1 | 6 | 70.250000 |
| 1 | 7 | 99.990000 |
| 1 | 8 | 99.990000 |
+------------+------------+-----------+
9 rows in set (0.33 sec)
----第3次编辑
似乎是基于jcrummacks查询的解决方案;
mysql> SELECT players_id, weapons_id, hits, shots, kills, MAX(acc) FROM ( SELECT players_id, weapons_id, SUM(hits) AS hits, SUM(shots) AS shots, SUM(kills) AS kills, AVG(acc) AS acc FROM player_weapon_stats GROUP BY players_id, weapons_id ORDER BY weapons_id ASC, AVG(acc) DESC) AS t1 GROUP BY weapons_id;
+------------+------------+------+-------+-------+-----------+
| players_id | weapons_id | hits | shots | kills | MAX(acc) |
+------------+------------+------+-------+-------+-----------+
| 202 | 0 | 1 | 3 | 0 | 25.000000 |
| 1544 | 1 | 1 | 0 | 0 | 0.000000 |
| 3034 | 2 | 8 | 11 | 0 | 84.995000 |
| 952 | 3 | 16 | 16 | 0 | 99.990000 |
| 3493 | 4 | 1 | 1 | 0 | 99.990000 |
| 839 | 5 | 33 | 35 | 2 | 94.290000 |
| 734 | 6 | 366 | 521 | 5 | 70.250000 |
| 2643 | 7 | 1 | 1 | 0 | 99.990000 |
| 3227 | 8 | 1 | 1 | 0 | 99.990000 |
+------------+------------+------+-------+-------+-----------+
9 rows in set (0.72 sec)
答案 0 :(得分:0)
我在这里阅读我认为你想要的内容,并假设你在一个相当新的版本的mysql(需要派生表支持),即使我不是你想要的可能这将指出你正确的方向。
select
players_id,
weapons_id,
max(acc)
from (
select
weapons_id,
players_id,
avg (acc) as acc
from
player_weapon_stats
group by
players_id,
weapons_id
order by
weapons_id asc,
avg(acc) desc) as t1
group by
weapons_id
希望我朝着你想去的方向前进。
答案 1 :(得分:0)
听起来好像你想要每个武器一排,任何玩家都可以达到最高精度。如果是这样,请尝试以下操作:
SELECT weapons_id,
SUM(hits) AS hits,
SUM(shots) AS shots,
SUM(kills) AS kills,
MAX(acc) AS acc
FROM (SELECT players_id,
weapons_id,
SUM(hits) AS hits,
SUM(shots) AS shots,
SUM(kills) AS kills,
FORMAT(SUM(hits) / SUM(shots), 4) * 100 AS acc
FROM player_weapon_stats
GROUP BY players_id, weapons_id) SQ
GROUP BY weapons_id