我有以下SQL代码:
SELECT
`table1`.`field1`
, `view1`.`newfield1`
FROM
`table1`
INNER JOIN `GCOTDA2`.`view1`
ON (`table1`.`id1` = `view1`.`id1`) AND (`table1`.`id2` = `view1`.`id2`);
查询工作正常,但现在我想将view1.newfield1复制到table1.field1。因此,我写了以下声明:
UPDATE `table1`
SET
`table1`.`field1` = `view1`.`newfield1`
FROM
`table1`
INNER JOIN `view1`
ON (`table1`.`id1` = `view1`.`id1`) AND (`table1`.`id2` = `view1`.`id2`);
但是,更新不起作用,我收到此错误消息:
Error Code: 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
`table1`
INNER JOIN `view1`
' at line 4
我在此网站上查看了谷歌和其他问题,例如:How do I UPDATE from a SELECT in SQL Server?和Error Code: 1064 in mysql但没有运气。 (MySQL Server 5.5.27) 我需要有人照亮我,谢谢!
答案 0 :(得分:1)
尝试使用此尺寸:
UPDATE 'table1` t JOIN `view1` v
ON t.id1 = v.id1 AND t.id2 = v.id2
SET t.field1 = v.newfield1
答案 1 :(得分:1)
您可以使用子查询执行此操作:
update
table1
set
table1.field1 = (select view1.newfield1 from view1 where view1.id1 = table1.id1 and view1.id2 = table1.id2)
where
exists (select null from view1 where view1.id1 = table1.id1 and view1.id2 = table1.id2)