首页 文章

mysql :: insert into table,来自另一个表的数据?

提问于
浏览
162

我想知道是否有办法在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将返回大约30k行 .

有没有办法在直接sql中做我正在尝试的内容?只是直接从一个表(基本上是原始数据表)中提取数据并插入另一个表(基本上是一个已处理的数据表)?

4 回答

  • 2
    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'
    
  • 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";
    
  • 344
    INSERT INTO Table1 SELECT * FROM Table2
    
  • 25

    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个名为 emailcurrent_time 的额外列 .

相关问题