我有一张看起来像的桌子
我需要特定team_id的目标总和
例如 :
如果team_id 16分别进球6和2,则在另一个新列sum_goal中的总和为8。
我无法继续。请帮助。
我尝试过的查询是:
选择(match_id,Sum(Goal)From
(
从match_result_updation中选择team1_id ID,team1_goal目标
联合所有
从match_result_updation中选择team2_id ID,team2_goal目标
)
as A)
从
(选择
c.tournament_id,g。*
来自team_trophies d
内部联接
比赛_比赛
上
e.tournament_id = d.tournament_id
内部联接
tour_scheduling c
上
c.tournament_id = e.tournament_id
内部联接
符合f
上
f.match_id = c.id
内部联接
match_result_updation g
上
g.match_id = f.id
哪里
f.match_type ='比赛'
和
d.tournament_id = 1)as p
按match_id分组
从第二个选择到id = 1,我得到的输出如附件图片所示,但我无法找到每个team_id的目标总和。
同样,如果组号16或17,则组1或组2的目标总和也将被计算。
最佳答案
我首先想到的是将它们放在相同的列中,然后求和。
编辑任何SQL引擎
Select ID, Sum(Goal) From
(
Select Team1_ID ID, Team1_Goal Goal From Table
Union All
Select Team2_ID ID, Team2_Goal Goal From Table
) A
Group By ID
ps如果将表格设计标准化,则不需要这种解决方法。
关于mysql - 在2个不同的列中为特定ID查找MYSQL中的max(sum),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42438264/