为什么我在SQL查询中收到此错误

时间:2012-08-27 20:18:29

标签: php sql

尝试此查询时:

 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(这是多少个goal_for)和team1(这是team_id)

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

由于

3 个答案:

答案 0 :(得分: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;

答案 1 :(得分: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)

答案 2 :(得分: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