mysql copy data from one table to another using foreign key

Andrei

inputPreviewOffsetLeft, inputPreviewOffsetTop, inputPreviewSizeWidth, inputPreviewSizeHeight are present in the display_preview table and display. display_preview has a foreign key displayId on display table.

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

D-Shih

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

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 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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to copy data from one table to another when foreign key is only defined in the destination table?

MySQL - Inserting Primary Key from one table to another (Foreign Key)

MYSQL Insert Data Into Table With Foreign Key From Another Table

Fetching Data from another table using foreign key laravel

MYSQL, copy data from one table to another table with increment in varchar

How to copy data from one table to another new table in MySQL?

how do i automatically fill in fields using data from another table (based on foreign key = primary key) in mysql workbench?

MySQL copy data from one table to another in different fields

How to copy structure of one table to another with foreign key constraints in psql?

Copy Data from one column to another column in a different table using MySQL

Copy Data from one table to another table

insert into 2 table one linked to the second through FOREIGN KEY and take data from another table in sqlite and python

How do I insert data into a table where one of the values should be a foreign key from another table?

Searching for a way to copy table data from one SQL Server database to another (concerning Identity and Foreign Keys)

update one table with data from another using derived key value

Copy Data from one hbase table to another

Copy table data from one database to another

How to retrieve ONE column from another table with Foreign key using Hibernate / JPA

Insert or Update Data into Table with Foreign Key from Another Table

Displaying data from one model and using a form to update another associated by foreign key

insert data from one table into another using a condition mysql

Transferring data from one table to another using a where clause mysql

How to copy data from one table to another using one by one row

How to copy data from one table to another and then delete that data in first table using Java?

How to copy records from one table to another without duplicates in MySQL where UNIQUE KEY is set

How to copy parent and child data from one database to another while assigning the newly created parent ids in the foreign key column of child tables?

How do I copy data from one table to another in postgres using copy command

How to show data from another table with foreign key CakePHP?

Foreign Key In Php to get data from another table