【问题标题】:sql - update several rows based on a specific row [closed]sql - 根据特定行更新几行[关闭]
【发布时间】:2012-08-06 18:42:25
【问题描述】:

我正在尝试从特定行的数据更新几行
这是一张表,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'

【问题讨论】:

  • 这有很多问题。您是否在此处查找了正确的语法:dev.mysql.com/doc/refman/5.0/en/update.html UPDATE table SET t.width=t1.width, t.repeat=t1.repeat WHERE t.ItemNum=# AND t.ItemNum=#;
  • 另外你是如何加入你的桌子的?你能展示一些你的mysql架构吗?

标签: mysql sql mysql-error-1064


【解决方案1】:

应该可以,但是如果 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  

【讨论】:

  • 对。他在这里需要一个连接子句。
  • 抱歉,添加了加入。自 MySQL 以来有一点点。希望有效
  • 仍然收到错误#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 t join (SELECT DISTINCT width, repeat FROM ' at line 5
  • @Phil 我认为他将无法进行此更新。从 t-q 上面对主要问题的回答来看,他正试图在他正在更新的同一张表上进行连接。 Mysql 不允许这样做。
  • 这里是一些额外的文档。您必须使用子查询作为临时表...forums.mysql.com/read.php?20,265513,265891
【解决方案2】:
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中指定支持表,方便更新语句

【讨论】:

  • 我认为这不是正确的 mysql 语法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-23
  • 1970-01-01
  • 1970-01-01
  • 2021-06-21
相关资源
最近更新 更多