简体   繁体   English

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

[英]mysql copy data from one table to another using foreign key

inputPreviewOffsetLeft , inputPreviewOffsetTop , inputPreviewSizeWidth , inputPreviewSizeHeight are present in the display_preview table and display . inputPreviewOffsetLeftinputPreviewOffsetTopinputPreviewSizeWidthinputPreviewSizeHeight存在于display_preview表和display中。 display_preview has a foreign key displayId on display table. display_previewdisplay表上有一个外键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

You don't need to get INSERT table an alias name and I think you might want to use INSERT INTO... SELECT on display_preview and display您不需要为INSERT表获取别名,我想您可能想在display_previewdisplay上使用INSERT INTO... SELECT

you can try the below query您可以尝试以下查询

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

EDIT编辑

If you want to do UPDATE... JOIN you can try this.如果你想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 

For more detail you can refer to MySQL UPDATE JOIN更多详情可以参考MySQL UPDATE JOIN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将一个表的外键约束复制到mysql中同一个数据库中的另一个 - copy the foreign key constraints of one table to another in the same db in mysql MySQL 外键约束:更新一个表时,用第一个表中的一些数据填充另一个表 - MySQL foreign key constraints : on updating one table, fill another table with some data from the first table MySQL - 将主键从一个表插入另一个表(外键) - MySQL - Inserting Primary Key from one table to another (Foreign Key) MySQL - 使用外键将行复制到另一个表 - MySQL - Copy row to another table with foreign key MySQL外键使用多个字段来引用另一张表中的主键 - MySQL foreign key using more than one field to reference to a primary key from another table MYSQL 使用另一个表的外键将数据插入表中 - MYSQL Insert Data Into Table With Foreign Key From Another Table mySQL SELECT 使用另一个表中的外键 - mySQL SELECT using a foreign key in another table 如何将数据从一个表复制到 MySQL 中的另一个新表? - How to copy data from one table to another new table in MySQL? MySql中如何将列数据从一张表复制到另一张表? - How to Copy Column data from one table to another table in MySql? MYSQL,将数据从一个表复制到另一个表,以varchar为增量 - MYSQL, copy data from one table to another table with increment in varchar
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM