使用INNER JOIN的UPDATE表 - 帮助!

时间:2011-06-28 13:43:16

标签: php mysql sql mysql-error-1052

我想更新表'tourneyteamsISF'字段'pointsfavor'和'pointscontra'来自表'tourneygamesISF'fiels'op1score'和'op2score'来自某个ID。

例如:

Table name = tourneygamesISF 

op1(name of row) vs. op2    -  op1score = 25 - op2score =  20

我想更新一下:

op1: (from: tourneygamesISF) = ID in tourneyteamsISF

+ pointsfavor = 25 (to: tourneyteamsISF)
+ pointscontra  = 20 (to: tourneyteamsISF)

op2: (from: tourneygamesISF) = ID in tourneyteamsISF

+ pointsfavor = 20 (to: tourneyteamsISF)
+ pointscontra  = 25 (to: tourneyteamsISF)

到目前为止,我有以下内容

UPDATE tourneyteamsISF 
INNER JOIN tourneygamesISF g1 ON (tourneyteamsISF.ID = g1.op1)
INNER JOIN tourneygamesISF g2 ON (tourneyteamsISF.ID = g2.op2)
SET pointsfavor = pointsfavor 
  + IF(g1.op1score > g1.op2score, g1.op1score - g1.op2score, 0) 
  + IF(g2.op2score > g2.op1score, g2.op2score - g2.op1score, 0)
, pointscontra = pointscontra 
  + IF(g1.op1score < g1.op2score, g1.op2score - g1.op1score, 0) 
  + IF(g2.op2score < g2.op1score, g2.op1score - g2.op2score, 0)
WHERE ID = 1;   

但是它给出了错误: where子句中的列'ID'不明确

我的表格是这样的:

TABLE tourneyteamsISF
  ID integer autoincrement primary key,
  name varchar,
  pointsfavor integer,
  pointscontra integer

TABLE tourneygamesISF
  ID integer autoincrement primary key,
  op1 integer,
  op2 integer,
  op1score integer, /*score for team1*/
  op2score integer /*score for team2*/

我知道它必须做一些事情来命名表团队和游戏的ID,但我感到困惑和困惑。我将非常感谢能得到的所有帮助。

2 个答案:

答案 0 :(得分:2)

WHERE ID = 1;&lt; - 这是你的问题。在它之前添加表名,它将编译没有任何问题。

例如。 WHERE g1.ID = 1;

发生此错误是因为SQL无法从ID语句中的表中识别您要使用的SELECT列。

答案 1 :(得分:1)

它必须是这样的 - WHERE g1.ID = 1