sql - 根据特定行更新多个行

时间:2012-08-06 18:42:26

标签: mysql sql mysql-error-1064

我正在尝试从特定行的数据更新多行 这是一张单人桌,ItemNum是唯一的 我的尝试:

UPDATE myTable t, (SELECT DISTINCT width, repeat
            FROM myTable
            WHERE ItemNum='80644') t1
SET t.width = t1.width
AND SET t.repeat = t1.repeat
WHERE ItemNum='80645'
AND WHERE ItemNum='80646'

给我一​​个错误 #1064 - 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 myTable WHERE ItemNum='80644') t1 SET t.width' at line 2


当我为一行运行它时,它的工作原理 但多行的语法似乎是问题

UPDATE myTable t, (SELECT DISTINCT width
  FROM myTable
  WHERE ItemNum='80644') t1
SET t.width = t1.width
WHERE ItemNum='80645'

2 个答案:

答案 0 :(得分:0)

应该可以工作,但是如果有多行且itemnum = 80644

,它将无法提供可靠的结果
UPDATE myTable t
join
    (SELECT DISTINCT width, repeat
            FROM myTable
            WHERE ItemNum='80644') t1
    on t.ItemNum in ('80645','80646')
SET
    t.width = t1.width,
    t.repeat = t1.repeat  

答案 1 :(得分:-1)

UPDATE t
SET t.width = t1.width
AND SET t.repeat = t1.repeat
FROM  myTable t INNER JOIN  (SELECT DISTINCT width, repeat
            FROM myTable
            WHERE ItemNum='80644') t1
ON ItemNum in ('80645'

, '80646')

update语句只能有一个表名 它必须更新的表

可以在join中指定

支持表以方便更新语句