0

I'm trying to find matches where the referee in the match (who is an official) also provides medical services to both the home and away teams (please refer to below relational diagram).

I've tried this, but it returns no records:

select matches.id,
matches.home,
matches.away,
matches.referee
from matches
join officials on officials.staffid = matches.referee
join medicalservices on medicalservices.team in (matches.home, matches.away)
where medicalservices.team = matches.home
and medicalservices.team = matches.away;

I'm guessing it doesn't return records because of the last two lines, but I don't know how else I can ensure that the same official provides medical services to both teams playing.

Here is the relational diagram for reference:

enter image description here

1 Answer 1

1

You need to join medical services twice, once to find services provided to the home team and again to find services provided to the away team. Then you can check if services provided to each team were by the same referee.

You also do not need the officals table.

select matches.id,
matches.home,
matches.away,
matches.referee
from matches
join medicalservices home_medservices on home_medservices.team  = matches.home
join medicalservices away_medservices on away_medservices.team = matches.away
where home_medservices.official = matches.referee
  and away_medservices.official = matches.referee
Sign up to request clarification or add additional context in comments.

2 Comments

Neither of these give the expected outputs.
@Tangler Sorry, try my updated answer. I didn't read your join clause before and assumed it was right.

Your Answer

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.