0

I am pretty new in SQL and have a question regarding a football database with the following tables:

Create Table Team(
TId char(3) Primary Key, // One example for a TId is "GER". It is an abbrevation for the country
Country varchar(50) Not Null,
Coach varchar(50) Not Null
)
Create Table Match(
MId Integer Primary Key, // Every match has an ID
Date date Null,
Stadium varchar(100) Not Null,
Team1 char(3) Not Null,
Team2 char(3) Not Null,
Foreign Key(Team1) references Team(TId),
Foreign Key(Team2) references Team(TId)
);
Create Table Goal(
MId Integer Null,
TId char(3) Null,
Player varchar(100) Not Null,
Minute Integer Null,
Primary Key(MId, Minute),
Foreign Key(MId) references Match(MId),
Foreign Key(TId) references Team(TId)
);

I need to write a SQL query that gives me a back a list of all matches with the date, the stadium and the number of goals both teams scored. It should be something like

Select date, stadium, Team1, Count(Goals Team1), Team2, Count(Goals Team2) 
From match
...

I tried to start to count the goals for one team first and wanted to use a case statement but did not come that far:

Select s.SId, s.Team1,
Case When s.SId IN (Select distinct SId From Tor) Then Count(t.SId) 
Else "Not In" 
End As text
From Spiel s, Tor t, Team te
Where s.SId = t.SId
Group by s.SId

I'd be pleased if you have any idea how to do that. Thanks in advance.

asked Jun 4, 2020 at 18:17
6
  • SELECT Match_id, SUM(Team_Id = {team 1}), SUM(Team_Id = {team 2}) FROM (Goals GROUP BY Match_id. Commented Jun 4, 2020 at 18:36
  • What is your version of Mysql? Commented Jun 4, 2020 at 19:09
  • You have a table called match? I don't believe you. Commented Jun 4, 2020 at 19:11
  • My version is 8.0.18 Commented Jun 4, 2020 at 19:19
  • And well, my table is not called "match" that's right. I translated it. You can call it "game" or something like that if you want ^^ Commented Jun 4, 2020 at 19:21

1 Answer 1

1

Use a CTE to return the total goals for each match and team.
Then join Match to 2 copies of Team and 2 copies of the CTE:

with cte as (
 select mid, tid, count(*) goals
 from Goal
 group by mid, tid
) 
select m.date, m.stadium,
 t1.tid team1, coalesce(c1.goals, 0) goals_1,
 t2.tid team2, coalesce(c2.goals, 0) goals_2
from `Match` m
inner join Team t1 on t1.tid = m.team1
inner join Team t2 on t2.tid = m.team2
left join cte c1 on c1.mid = m.mid and c1.tid = m.team1
left join cte c2 on c2.mid = m.mid and c2.tid = m.team2
answered Jun 4, 2020 at 19:26
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.