我一直试图让以下 select 语句工作几天,但没有任何运气。代码的快速总结是,它返回数据库中所有球队的列表,以及每支球队与排名前 10 名球队的胜利次数。胜利是从包含已玩过的每场比赛的比赛表中提取的。这是我的代码
SELECT IF(@last_ranking <> formula, @current_rankings := @current_rankings + 1, @current_rankings) AS rank,
@last_ranking := formula,
prequery.team,
prequery.formula
FROM (select @current_rankings := 0) sqlvars,
(SELECT team,
SUM(score) formula
FROM (SELECT g.home_team,
SUM(CASE WHEN t.team_id IS NULL THEN 0 ELSE g.home_wins END) AS formula
FROM games g LEFT JOIN
_top_10 AS t ON g.away_team = t.team_id
WHERE
g.season = 2012 AND
g.completed = 1
UNION ALL SELECT g.away_team,
SUM(CASE WHEN t2.team_id IS NULL THEN 0 ELSE g.away_wins END) AS formula
FROM games g LEFT JOIN
_top_10 AS t2 ON g.home_team = t2.team_id
WHERE
g.season = 2012 AND
g.completed = 1),
(SELECT @current_rankings := 0, @last_ranking := 0) r
GROUP BY team ORDER BY formula DESC ) prequery;
但我不断收到以下错误
错误代码:1248。每个派生表都必须有自己的别名
谁能看到我在这里做错了什么。谢谢!