我有3个表,table1和table2有数据。我需要将这些数据推送/插入到其他表中。
PFA图片:
PLZ帮助我获得最终结果表
提前致谢
我尝试了node.js
这里table1是companyAsc,table2 personAsc和table3 country_associate
connection.query(' SELECT * FROM companyAsc',function(err,rows,fields) {
if (!err) {
if(rows.length == 0){
}else{
var str = JSON.stringify(rows)
var parsed2 = JSON.parse(str);
connection.query('SELECT * FROM personAsc', function(err, rows, fields)
{
if (!err) {
if(rows.length == 0){
} else{
var str = JSON.stringify(rows)
var parsed3 = JSON.parse(str);
//company_id = parsed2[0].id;
if(parsed2.length>parsed3.length){
console.log("1")
for(i=0;i<parsed2.length;i++){
var person_id;
var company_id = parsed2[i].company_id;
var country_id = parsed2[i].country_id;
if(parsed3[i]!=undefined){
person_id = parsed3[i].person_id;
}else{
person_id = 3725;
}
connection.query('insert into country_associate(country_id,company_id,person_id) values ("'+country_id+'","'+company_id+'","'+person_id+'")',function(err, rows, fields){
if (err) console.log(err);
else{
console.log("inserted");
}
});
}
} else{
for(i=0;i<parsed3.length;i++){
var company_id;
//var person_id = parsed3[i].person_id;
var country_id = parsed3[i].country_id;
if(parsed2[i]!=undefined){
company_id = parsed2[i].company_id;
}else{
company_id = 1317;
}
if(parsed3[i]!=undefined){
person_id = parsed3[i].person_id;
}else{
person_id = 3725;
}
connection.query('insert into country_associate(country_id,company_id,person_id) values ("'+parsed2[i].country_id+'","'+company_id+'","'+person_id+'")',function(err, rows, fields){
if (err) console.log(err);
else{
console.log("inserted");
}
});
}
}
}
}else{
console.log(err)
}
});
}
}else{
console.log(err)
}
});
答案 0 :(得分:1)
由于MySql不支持完全外连接使用union,因此它们是等效的
INSERT INTO Table3 (A, B, C)
SELECT Table1.A, Table1.B, Table2.C FROM Table1 t1
LEFT JOIN Table2 t2 ON t1.A = t2.A
UNION
SELECT Table1.A, Table1.B, Table2.C FROM Table1 t1
RIGHT JOIN Table2 t2 ON t1.A = t2.A
答案 1 :(得分:0)
insert into table3 (a, b, c)
(SELECT table1.a, table1.b, table2.c
from table1, table2
where table1.a = table2.a);
您的问题不明确,您尝试了什么?