I am working on developing sports site.
In this site I've two tables one is tbl_team
which stores the team details and another table is tbl_tournamentmatches
which stores the details of different rounds of tournament matches.
In my tbl_tournamentmatches
I'm storing team1_id
, team2_id
, team1_score
and team2_score
.
My Table having entry like this:
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
I want Result Should look like this:-
Team name Score
CSK 35
KKR 25
RR 20
I'd used this query :-
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
But i got result like this :-
KKR 25
RR 15
RR 5
CSK 35
Any Help will be appreciated. Thanks in advance.
Do the joins to get the team and points for each match, and then do the sum on the results of that:-
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
try this query
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
Use Union all
to make all team and score as different rows.
Then use an Inner Join
.
Query
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;
You almost got it.
In your query I see fields that you don't include in your table (eg agegroup_id), but you only are a step of a solution: change your outter "select * from " for a: "select sum(totalpoints), team_title" and add a group by at the end: " 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;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.