SQL Insert data from multiple tables in different

2019-03-06 10:42发布

问题:

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 ;

回答1:

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.