SQL从不同数据库中的多个表插入数据 [英] SQL Insert data from multiple tables in different database

查看:532
本文介绍了SQL从不同数据库中的多个表插入数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在a2数据库中有一个猫表,我想将其插入具有不同数据库的不同表的id和img列中

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

插入a2.cat(id,img)从topshop_test.product中选择ID, 从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.

这篇关于SQL从不同数据库中的多个表插入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆