我正在努力寻找解决这个问题的方法,但我看不到一种方法。我正在插入游戏中的杀戮,我正在记录凶手,他的氏族,受害者和受害者的氏族,所以我的数据库看起来像这样,killer | killerClan | victim | victimClan-----------------------------------------User_A | Team_A | User_B | Team_BUser_C | Team_B | User_A | Team_AUser_B | Team_B | User_A | Team_AUser_D | Team_A | User_C | Team_BUser_C | Team_B | User_A | Team_AUser_B | Team_B | User_D | Team_AUser_D | Team_A | User_C | Team_B同一用户可以显示为杀手或受害者,这取决于他是否杀死了某人或被某人杀死。我可以通过以下方式显示特定氏族的用户的总杀戮,SELECT killer AS username, COUNT(killer) AS kills FROM master_trackerWHERE killerClan = "Team_A"GROUP BY killerORDER BY kills DESC通过同样的逻辑,我可以通过以下方式显示特定氏族的用户的总死亡人数,SELECT victim AS username, COUNT(victim) AS deaths FROM master_trackerWHERE victimClan = "Team_A"GROUP BY victimORDER BY deaths DESC但。。。我想做的是将这两个查询结合起来,并按他们的名字显示用户的杀戮和死亡组,这可以在第一种情况下的杀手列和第二种情况下的受害者列中找到,因此最终能够呈现他们的杀戮/死亡比率,但我需要不同的位置原因,因为在第一种情况下,他们的氏族可以被发现为杀手族,在第二种情况下可以找到受害者族。目标是获得这样的结果。username | kills | deaths | kdRatio----------------------------------- User_D | 2 | 1 | 1 User_A | 1 | 3 | -2欢迎所有建议。
1 回答
holdtom
TA贡献1805条经验 获得超10个赞
您可以使用和聚合:union all
select
username,
sum(kills) kills,
sum(deaths) deaths,
sum(kills) - sum(deaths) kdRatio
from (
select killer username, count(*) kills, 0 deaths
from master_tracker
where killerClan = 'TeamA'
group by killer
union all
select victim, 0, count(*)
from master_tracker
where victimClan = 'TeamA'
group by victim
) t
group by username
order by kdRatio desc
- 1 回答
- 0 关注
- 85 浏览
添加回答
举报
0/150
提交
取消