SQL从不同数据库中的多个表中插入数据

2022-01-09 00:00:00 sql database insert mysql sql-insert

我在 a2 数据库中有 cat 表,我想从不同数据库的不同表中插入 id、img 列

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 ,从 topshop_test.product-images 中选择名称;

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

推荐答案

我认为您需要考虑为此使用 JOIN:

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

这假设 product-images 表有一个 productid 字段链接到 product 表.

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

相关文章