Your subquery is correct, but when same abbreviated name has more than one id's it returns more than one row, which will give error. Example:
id | abbreviated_name
----------------------
1 | ATL
4 | ATL
in
satisfies this case.
Also join
is not needed when using sub query, which will create extra records when join on
conditions don't match
SELECT *
FROM schedule s
WHERE s.home_team_id in (
SELECT id
FROM team
WHERE team.abbreviated_name = 'ATL'
)
OR s.visitor_team_id in (
SELECT id
FROM team
WHERE team.abbreviated_name = 'ATL'
)
This is join
version
SELECT *
FROM schedule s
LEFT JOIN team home_t on s.home_team_id=home_t.id
LEFT JOIN team visitor_t on s.visitor_team_id=visitor_t.id
WHERE home_t.abbreviated_name = 'ATL'
OR visitor_t.abbreviated_name = 'ATL'
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…