0

我有以下 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

我在这个网站上查看了谷歌和其他问题,例如:如何从 SQL Server 中的 SELECT 更新?错误代码:mysql中的 1064 没有运气。(MySQL Server 5.5.27) 我需要有人来照亮我,谢谢!

4

2 回答 2

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)

http://sqlfiddle.com/#!2/64774/1/0

于 2013-11-01T16:40:42.413 回答
1

试穿这个尺寸:

UPDATE 'table1` t JOIN `view1` v 
ON      t.id1 = v.id1 AND t.id2 = v.id2
SET     t.field1 = v.newfield1
于 2013-11-01T16:37:43.863 回答