JS-Hero
JS-Hero

Reputation: 317

SQL Insert data from multiple tables in different database

I have cat table in a2 database I want to insert into id, img columns from different tables with different database

INSERT INTO a2.cat (id, img) SELECT id FROM topshop_test.product , SELECT name FROM topshop_test.product-images ;

Upvotes: 0

Views: 1434

Answers (1)

sgeddes
sgeddes

Reputation: 62831

I think you need to look into using a JOIN for this:

INSERT INTO a1.cat (id, img)
SELECT p.id, pi.name
FROM topshop_test.product p 
    JOIN topshop_test.product-images pi ON p.id = pi.productid

This assumes the product-images table has a productid field that links to the product table.

Upvotes: 2

Related Questions