mysql 中相同 table 的多列求和
sum multiple columns of same table in mysql
我正在开发体育网站。
在这个站点我有两个 table,一个是 tbl_team
,它存储球队的详细信息,另一个 table 是 tbl_tournamentmatches
,它存储不同回合的详细信息的比赛。
在我的 tbl_tournamentmatches
中,我正在存储 team1_id
、team2_id
、team1_score
和 team2_score
。
我的 Table 有这样的条目:
tbl_tournamentmatches
match_id team1_id team2_id team1_score team2_score
5 4 9 15 5
6 9 16 15 5
7 4 16 5 15
8 4 16 5 15
tbl_team
team_id team_title
4 KKR
9 RR
16 CSK
我想要结果应该是这样的:-
Team name Score
CSK 35
KKR 25
RR 20
我用过这个查询:-
select * from
(
SELECT sum(team1_points) as totalpoints,t.team_title
from tbl_team t
left join tbl_tournamentmatches m
on t.team_id = m.team1_id
where tournament_id = 3 AND agegroup_id = 36
group by team1_id
union
SELECT sum(team2_points) as totalpoints,t.team_title
from tbl_team t
left join tbl_tournamentmatches m
on t.team_id = m.team2_id
where tournament_id = 3 AND agegroup_id = 36
group by team2_id
) s
但我得到了这样的结果:-
KKR 25
RR 15
RR 5
CSK 35
任何帮助将不胜感激。提前致谢。
进行连接以获得每场比赛的球队和积分,然后对结果进行求和:-
SELECT team_title, sum(team_points) as totalpoints
FROM
(
SELECT team1_id AS team_id, team1_points AS team_points, t.team_title
FROM tbl_team t
LEFT JOIN tbl_tournamentmatches m
ON t.team_id = m.team1_id
WHERE tournament_id = 3 AND agegroup_id = 36
UNION ALL
SELECT team2_id AS team_id, team2_points AS team_points, t.team_title
FROM tbl_team t
LEFT JOIN tbl_tournamentmatches m
ON t.team_id = m.team2_id
WHERE tournament_id = 3 AND agegroup_id = 36
) s
GROUP BY team_id, team_title
使用Union all
使所有队伍和得分为不同的行。
然后使用 Inner Join
.
查询
select t2.team_title as TeamName,
sum(team_score) as Score
from
(
select match_id,team1_id as team_id, team1_score as team_score
from tblMatch
union all
select match_id,team2_id as team_id, team2_score as team_score
from tblMatch
)t1
join tblTeam t2
on t1.team_id = t2.team_id
group by t2.team_id;
试试这个查询
SELECT t.team_name
,SUM(CASE
WHEN t.team_id = tn.team1_id THEN tn.team1_score
WHEN t.team_id = tn.team2_id THEN tn.team2_score
END) AS score
FROM team t
LEFT JOIN tournament tn ON t.team_id = tn.team1_id OR t.team_id = team2_id
GROUP BY t.team_name
你差不多明白了。
在您的查询中,我看到您未包含在 table 中的字段(例如 agegroup_id),但您只是解决方案的一个步骤:更改您的外部 "select * from " for a: "select sum(totalpoints), team_title" 并在末尾添加一个group by: "group by team_title
select sum(totalpoints) as totalpoints, team_title from
(
SELECT sum(team1_score) as totalpoints, t.team_title
from tbl_team t
join tbl_tournamentmatches m
on t.team_id = m.team1_id
--where tournament_id = 3 AND agegroup_id = 36
group by team1_id
union
SELECT sum(team2_score) as totalpoints, t.team_title
from tbl_team t
join tbl_tournamentmatches m
on t.team_id = m.team2_id
--where tournament_id = 3 AND agegroup_id = 36
group by team2_id
) s
group by team_title;
我正在开发体育网站。
在这个站点我有两个 table,一个是 tbl_team
,它存储球队的详细信息,另一个 table 是 tbl_tournamentmatches
,它存储不同回合的详细信息的比赛。
在我的 tbl_tournamentmatches
中,我正在存储 team1_id
、team2_id
、team1_score
和 team2_score
。
我的 Table 有这样的条目:
tbl_tournamentmatches
match_id team1_id team2_id team1_score team2_score
5 4 9 15 5
6 9 16 15 5
7 4 16 5 15
8 4 16 5 15
tbl_team
team_id team_title
4 KKR
9 RR
16 CSK
我想要结果应该是这样的:-
Team name Score
CSK 35
KKR 25
RR 20
我用过这个查询:-
select * from
(
SELECT sum(team1_points) as totalpoints,t.team_title
from tbl_team t
left join tbl_tournamentmatches m
on t.team_id = m.team1_id
where tournament_id = 3 AND agegroup_id = 36
group by team1_id
union
SELECT sum(team2_points) as totalpoints,t.team_title
from tbl_team t
left join tbl_tournamentmatches m
on t.team_id = m.team2_id
where tournament_id = 3 AND agegroup_id = 36
group by team2_id
) s
但我得到了这样的结果:-
KKR 25
RR 15
RR 5
CSK 35
任何帮助将不胜感激。提前致谢。
进行连接以获得每场比赛的球队和积分,然后对结果进行求和:-
SELECT team_title, sum(team_points) as totalpoints
FROM
(
SELECT team1_id AS team_id, team1_points AS team_points, t.team_title
FROM tbl_team t
LEFT JOIN tbl_tournamentmatches m
ON t.team_id = m.team1_id
WHERE tournament_id = 3 AND agegroup_id = 36
UNION ALL
SELECT team2_id AS team_id, team2_points AS team_points, t.team_title
FROM tbl_team t
LEFT JOIN tbl_tournamentmatches m
ON t.team_id = m.team2_id
WHERE tournament_id = 3 AND agegroup_id = 36
) s
GROUP BY team_id, team_title
使用Union all
使所有队伍和得分为不同的行。
然后使用 Inner Join
.
查询
select t2.team_title as TeamName,
sum(team_score) as Score
from
(
select match_id,team1_id as team_id, team1_score as team_score
from tblMatch
union all
select match_id,team2_id as team_id, team2_score as team_score
from tblMatch
)t1
join tblTeam t2
on t1.team_id = t2.team_id
group by t2.team_id;
试试这个查询
SELECT t.team_name
,SUM(CASE
WHEN t.team_id = tn.team1_id THEN tn.team1_score
WHEN t.team_id = tn.team2_id THEN tn.team2_score
END) AS score
FROM team t
LEFT JOIN tournament tn ON t.team_id = tn.team1_id OR t.team_id = team2_id
GROUP BY t.team_name
你差不多明白了。
在您的查询中,我看到您未包含在 table 中的字段(例如 agegroup_id),但您只是解决方案的一个步骤:更改您的外部 "select * from " for a: "select sum(totalpoints), team_title" 并在末尾添加一个group by: "group by team_title
select sum(totalpoints) as totalpoints, team_title from
(
SELECT sum(team1_score) as totalpoints, t.team_title
from tbl_team t
join tbl_tournamentmatches m
on t.team_id = m.team1_id
--where tournament_id = 3 AND agegroup_id = 36
group by team1_id
union
SELECT sum(team2_score) as totalpoints, t.team_title
from tbl_team t
join tbl_tournamentmatches m
on t.team_id = m.team2_id
--where tournament_id = 3 AND agegroup_id = 36
group by team2_id
) s
group by team_title;