使用其他查询更新联接中的表?

时间:2012-04-05 21:09:26

标签: mysql sql sql-update

我有桌子'球员'和'奖项'。我有一些重复的播放器条目具有不同的ID,但相同的信息。

我正在尝试更新与第二个玩家条目相关联的奖励,以便最终删除重复项。

这显然不是最有效的查询,但它只需要运行一次,并且只有10-15个重复。 到目前为止,我有这个:

UPDATE aw
SET aw.player_id = map.id1
FROM awards as aw,
    (SELECT a.id as id1, b.id as id2
    FROM players a, players b
    WHERE a.first_name = b.first_name 
    AND a.last_name = b.last_name 
    AND a.id != b.id 
    AND a.team is not null 
    AND a.college is not null) as map
WHERE map.id2 = aw.player_id

这给了我一个未指定的语法错误,但如果我用just替换前两行 “选择aw.player_id,map.id1”,它返回一个带有预期值的两列表(因此列名和表引用都是正确的。)

我必须遗漏一些明显的东西,但它是什么?

2 个答案:

答案 0 :(得分:3)

UPDATE syntax中没有FROM。

您应该在SET

之前的UPDATE部分中定义表
UPDATE awards as aw,
  (SELECT a.id as id1, b.id as id2
  FROM players a, players b
  WHERE a.first_name = b.first_name 
  AND a.last_name = b.last_name 
  AND a.id != b.id 
  AND a.team is not null 
  AND a.college is not null) as map
SET aw.player_id = map.id1
WHERE map.id2 = aw.player_id

答案 1 :(得分:0)

这应该在语法上是正确的。但是,我不确定它是否符合您的要求。 首先测试暂存数据!

UPDATE
  players AS a 
INNER JOIN
  players AS b
ON
  a.first_name = b.first_name
  AND a.last_name = b.last_naem
  AND a.id != b.id
  AND a.team IS NOT NULL
  AND a.college IS NOT NULL
INNER JOIN
  awards AS aw
ON
  b.id = aw.player_id
SET
  aw.player_id = a.id