mySQL:: insert到表,数据从另一个表?

我想知道是否有一种方法可以在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上面? 只是直接从一个表(基本上是一个原始数据表)提取数据并插入到另一个表(基本上是一个处理过的数据表)?< / p >

306623 次浏览
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'

for整行

insert into xyz select * from xyz2 where id="1";

for selected column

insert into xyz(t_id,v_id,f_name) select t_id,v_id,f_name from xyz2 where id="1";
INSERT INTO Table1 SELECT * FROM Table2

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'

在上面的查询中,有两个额外的列,名为电子邮件 &当前时间

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

分析< em > < / em >

在mysql中,如果我们想将数据从一个表复制到另一个表,可以使用上面的查询

  1. 这里源表和目标表是相同的,我们也可以使用不同的表。
  2. 我们没有复制一些列,比如style_idis_deleted,所以我们从另一个表中硬编码选择它们
  3. 我们在源中使用的表也包含自动增量字段,因此我们离开该列,它会自动插入查询的执行。

< em >执行结果< / em >

1个查询执行,1个成功,0个错误,0个警告

查询:insert into primarary_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'";