:插入到表中,来自另一个表的数据?

时间:2022-09-15 22:23:55

I was wondering if there is a way to do this purely in sql:

我想知道是否有一种方法可以用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);

Note: q1 would return about 30k rows.

注意:q1将返回大约30k行。

Is there any way to do what I am attempting above in straight sql? To just pull the data straight from one table (basically a raw data table) and insert into another table (basically a processed data table)?

有什么方法可以直接使用sql来完成上面的操作吗?将数据从一个表(基本上是一个原始数据表)直接拉到另一个表(基本上是一个处理过的数据表)?

4 个解决方案

#1


323  

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'

#2


21  

for whole row

为整个行

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";

#3


7  

Answered by zerkms is the correct method. But, if someone looking to insert more extra column in the table then you can get it from the following:

用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'

In the above query, there are 2 extra columns named email & current_time.

在上面的查询中,有两个额外的列名为email & current_time。

#4


2  

INSERT INTO Table1 SELECT * FROM Table2

#1


323  

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'

#2


21  

for whole row

为整个行

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";

#3


7  

Answered by zerkms is the correct method. But, if someone looking to insert more extra column in the table then you can get it from the following:

用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'

In the above query, there are 2 extra columns named email & current_time.

在上面的查询中,有两个额外的列名为email & current_time。

#4


2  

INSERT INTO Table1 SELECT * FROM Table2