我正在运行以下查询,但在“r.teamId”上出现无效引用错误。我找到了很多关于这个的答案,但是大多数都建议将隐式连接转换为显式连接。在我的例子中,我认为查询已经显式连接了。如何解决此错误?
select r.teamId,sum(r.amount),count(r.id),sum(r.distance),s.start_date
from runs r inner join ( SELECT team.id,league.start_date,league.end_date FROM league join team
ON league.id =team.league_id where r.teamId = team.id) s
on s.id = r.teamId
where r.teamId IS Not NULL
group by r.teamId,s.start_date
order by sum(r.amount) desc;
最佳答案
去掉无用的括号和派生表,就不会被错误的别名所迷惑:
select r.teamId,
sum(r.amount),
count(r.id),
sum(r.distance),
l.start_date
from runs r
join team on r.teamid = team.id
join league l on l.id = team.league_id
where r.teamId IS Not NULL
group by r.teamId, l.start_date
order by sum(r.amount) desc;