我可能忽略了一些非常明显的内容,但是我无法运行多个UPDATE
语句。我要实现的是从一个表中SELECT
的一个AVG()
值和一个基于另一个表中的这些值的UPDATE
列。
我遵循以下解决方案,但无法使其运行:
我的解决方案:
UPDATE
teams
SET
teams.overall_rank = q.rank_avg
FROM
(
SELECT
team_id as t_id,
AVG(rank) as rank_avg
FROM
team_list
WHERE
rank NOT IN (0, -1)
GROUP BY
team_id
ORDER BY
rank_avg
) q
WHERE
q.t_id = teams.team_id
错误消息:
You have an error in your SQL syntax; it seems the error is around: 'FROM ( SELECT team_id as t_id, AVG(rank) as rank_avg FR' at line 5
我正在使用MySQL。我在这里想念什么?
答案 0 :(得分:1)
尝试以下方法-您的语法对mysql无效
UPDATE teams
join
(
SELECT
team_id as t_id,AVG(rank) as rank_avg
FROM team_list
WHERE rank NOT IN (0, -1)
GROUP BY team_id
) q on q.t_id = teams.team_id
SET teams.overall_rank = q.rank_avg
答案 1 :(得分:1)
我认为您需要加入:
UPDATE teams t INNER JOIN (
SELECT
team_id as t_id,
AVG(rank) as rank_avg
FROM team_list
WHERE rank NOT IN (0, -1)
GROUP BY team_id
) q ON q.t_id = t.team_id
SET t.overall_rank = q.rank_avg
不需要子查询中的ORDER BY。