我想知道是否有办法纯粹在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中执行我上面尝试的操作? 只是直接从一个表(基本上是原始数据表)中提取数据并插入另一个表(基本上是一个已处理的数据表)?
答案 0 :(得分:365)
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'
答案 1 :(得分:26)
整行
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";
答案 2 :(得分:9)
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个额外的列名为电子邮件&的的current_time 强>
答案 3 :(得分:2)
INSERT INTO Table1 SELECT * FROM Table2
答案 4 :(得分:0)
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中的另一表,则可以使用上面的查询
执行结果
已执行1个查询,1个成功,0个错误,0个警告
查询:插入primary_image(style_id,pre_image_status,file_extension,reviewer_id,uploader_id,is_deleted,last_updated)中选择...
5行受到影响
执行时间:0.385秒 传输时间:0秒 总时间:0.386秒
答案 5 :(得分:0)
此查询用于将数据从一张表添加到另一张表 使用外键
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")
}
})
}
})
}
}
}
})
})