mysql 使用外键将数据从一个 table 复制到另一个

mysql copy data from one table to another using foreign key

inputPreviewOffsetLeftinputPreviewOffsetTopinputPreviewSizeWidthinputPreviewSizeHeight 出现在 display_preview、table 和 display 中。 display_previewdisplay table 上有一个外键 displayId

INSERT INTO display_preview b (b.inputPreviewOffsetLeft, b.inputPreviewOffsetTop, b.inputPreviewSizeWidth, b.inputPreviewSizeHeight)
    SELECT bd.inputPreviewOffsetLeft, bd.inputPreviewOffsetTop, bd.inputPreviewSizeWidth, bd.inputPreviewSizeHeight 
    FROM display
INNER JOIN display bd on bd.id = b.displayId
Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'b (b.inputPreviewOffsetLeft, b.inputPreviewOffsetTop, b.inputPreviewSizeWidth...' at line 1

您不需要 INSERT table 别名,我想您可能想在 display_previewdisplay 上使用 INSERT INTO ... SELECT

你可以试试下面的查询

INSERT INTO display_preview (inputPreviewOffsetLeft,inputPreviewOffsetTop, inputPreviewSizeWidth, inputPreviewSizeHeight)
SELECT bd.inputPreviewOffsetLeft, bd.inputPreviewOffsetTop, bd.inputPreviewSizeWidth, bd.inputPreviewSizeHeight 
FROM display_preview b
INNER JOIN display bd on bd.id = b.displayId

编辑

如果你想做 UPDATE ... JOIN 你可以试试这个。

UPDATE display_preview b
INNER JOIN display bd on bd.id = b.displayId
SET 
    b.inputPreviewOffsetLeft = bd.inputPreviewOffsetLeft, 
    b.inputPreviewOffsetTop = bd.inputPreviewOffsetTop, 
    b.inputPreviewSizeWidth = bd.inputPreviewSizeWidth , 
    b.inputPreviewSizeHeight = bd.inputPreviewSizeHeight 

更多细节可以参考MySQL UPDATE JOIN