我想知道是否有一种方法可以纯粹在 sql 中执行此操作:
q1 = SELECT campaign_id, from_number, received_msg, date_received
FROM `received_txts` WHERE `campaign_id` = '8';
INSERT INTO action_2_members (campaign_id, mobile, vote, vote_date)
VALUES(q1.campaign_id, q1.from_number, q1.received_msg, q1.date_received);
注意:q1 将返回大约 30k 行。
有什么方法可以在直接 sql 中执行我在上面尝试的操作吗?直接从一个表(基本上是原始数据表)中提取数据并插入另一个表(基本上是处理过的数据表)?
INSERT INTO action_2_members (campaign_id, mobile, vote, vote_date)
SELECT campaign_id, from_number, received_msg, date_received
FROM `received_txts`
WHERE `campaign_id` = '8'
整行
insert into xyz select * from xyz2 where id="1";
对于选定的列
insert into xyz(t_id,v_id,f_name) select t_id,v_id,f_name from xyz2 where id="1";
zerkms 的回答是正确的方法。但是,如果有人希望在表中插入更多额外的列,那么您可以从以下获取它:
INSERT INTO action_2_members (`campaign_id`, `mobile`, `email`, `vote`, `vote_date`, `current_time`)
SELECT `campaign_id`, `from_number`, 'example@domain.xyz', `received_msg`, `date_received`, 1502309889 FROM `received_txts` WHERE `campaign_id` = '8'
在上面的查询中,有 2 个额外的列名为 email 和 current_time。
INSERT INTO Table1 SELECT * FROM Table2
INSERT INTO preliminary_image (style_id,pre_image_status,file_extension,reviewer_id,
uploader_id,is_deleted,last_updated)
SELECT '4827499',pre_image_status,file_extension,reviewer_id,
uploader_id,'0',last_updated FROM preliminary_image WHERE style_id=4827488
分析
如果我们想将数据从一个表复制到mysql中的另一个表,我们可以使用上面的查询
这里源表和目标表相同,我们也可以使用不同的表。我们没有像 style_id 和 is_deleted 那样复制的几列,所以我们从另一个表中硬编码选择了它们 我们在源代码中使用的表也包含自动增量字段,所以我们离开了该列,它在执行查询时自动插入。
执行结果
执行了 1 个查询,1 个成功,0 个错误,0 个警告
查询:插入到primary_image (style_id,pre_image_status,file_extension,reviewer_id,uploader_id,is_deleted,last_updated) select ...
5 行受影响
执行时间:0.385 秒传输时间:0 秒总时间:0.386 秒
此查询用于使用外键将数据从一个表添加到另一个表
let qry = "INSERT INTO `tb_customer_master` (`My_Referral_Code`, `City_Id`, `Cust_Name`, `Reg_Date_Time`, `Mobile_Number`, `Email_Id`, `Gender`, `Cust_Age`, `Profile_Image`, `Token`, `App_Type`, `Refer_By_Referral_Code`, `Status`) values ('" + randomstring.generate(7) + "', '" + req.body.City_Id + "', '" + req.body.Cust_Name + "', '" + req.body.Reg_Date_Time + "','" + req.body.Mobile_Number + "','" + req.body.Email_Id + "','" + req.body.Gender + "','" + req.body.Cust_Age + "','" + req.body.Profile_Image + "','" + req.body.Token + "','" + req.body.App_Type + "','" + req.body.Refer_By_Referral_Code + "','" + req.body.Status + "')";
connection.query(qry, (err, rows) => {
if (err) { res.send(err) } else {
let insert = "INSERT INTO `tb_customer_and_transaction_master` (`Cust_Id`)values ('" + rows.insertId + "')";
connection.query(insert, (err) => {
if (err) {
res.json(err)
} else {
res.json("Customer added")
}
})
}
})
}
}
}
})
})
$insertdata="insert into partner_products(partner_id,partner_category_id,main_category_id, inventory_id,partner_product_name, partner_product_brand, partner_product_price,partner_product_quantity,partner_product_unit) select '123',partner_category_id,main_category_id,inventory_id, item_name,brand_name,item_price,item_qty, item_unit from inventory where partner_category_id='1'";
INSERT INTO ... SELECT
语句一起使用吗?INSERT