1

尝试此查询时:

 UPDATE l1_a
 SET l1_a.goals_for = l1_a.goals_for + l1_results.score1
 FROM l1_results, l1_a
 WHERE l1_results.team1 = l1_a.team_id;

我得到错误:

   You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM l1_results, l1_a WHERE l1_results.team1 = l1_a.team_id' at line 3

l1_a 包含goals_for 和team_id

l1_results 包含 score1(这是多少个进球)和 team1(这是 team_id)

我的查询有问题吗?我已经仔细检查了所有列和表都是查询中的正确名称

谢谢

4

3 回答 3

4

声明这documentation for UPDATE是多表UPDATE语句的语法:

Multiple-table syntax:

UPDATE [LOW_PRIORITY] [IGNORE] table_references
    SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] ...
    [WHERE where_condition]

试试这个:

 UPDATE l1_results, l1_a
 SET l1_a.goals_for = l1_a.goals_for + l1_results.score1
 WHERE l1_results.team1 = l1_a.team_id;
于 2012-08-27T20:21:05.673 回答
1

您正在结合选择和更新语法。更新语句中没有from

试试这个:

UPDATE l1_a
SET l1_a.goals_for = l1_a.goals_for + 
  (select l1_results.score1 
  from l1_results 
  where l1_results.team1 = l1a.team_id)

如果 l1_results 中没有记录,您可能会清空数据。最好添加一个 IFNULL:

UPDATE l1_a
SET l1_a.goals_for = l1_a.goals_for + IFNULL(
  (select l1_results.score1 
  from l1_results 
  where l1_results.team1 = l1a.team_id), 0)
于 2012-08-27T20:20:55.360 回答
0

改变

UPDATE l1_a
SET l1_a.goals_for = l1_a.goals_for + l1_results.score1
FROM l1_results, l1_a
WHERE l1_results.team1 = l1_a.team_id;

UPDATE l1_a
SET l1_a.goals_for = l1_a.goals_for + l1_results.score1
WHERE l1_results.team1 = l1_a.team_id;

你不需要 FROM l1_results, l1_a

于 2012-08-27T20:38:47.340 回答