mysql ::插入到表中,从另一个表中的数据?
我想知道是否有一种方法来纯粹在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'
整行
插入xyz select * from xyz2 where id =“1”;
为选定的列
插入xyz(t_id,v_id,f_name)从xyz2中selectt_id,v_id,f_name其中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'
在上面的查询中,有两个额外的列名为email & current_time 。
INSERT INTO Table1 SELECT * FROM Table2
你可以这样做,如下所示:
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'