如何获取SELECTed输出并将其写入MySQL中的新TABLE?

时间:2012-10-04 22:30:04

标签: mysql sql sql-insert select-insert

  

可能重复:
  Insert data from to a table

从主Tax表中,我希望能够进行计算并将输出写入新表。

select zipcode, sum(A00100)/sum(N1) as 'avg_agi' from taxbyzip2008 group by zipcode;

名为avgagi的新表将包含两个字段(列),zipcode和avg_agi。

我知道如何创建一个新表,但我不知道如何从上面的select中获取输出并将其写入新表avgagi并用zipcode和avg_agi填充它。谢谢!

2 个答案:

答案 0 :(得分:1)

执行您的创建表,然后

insert into avgagi
select zipcode, sum(A00100)/sum(N1) as 'avg_agi' from taxbyzip2008 group by zipcode;

答案 1 :(得分:1)

insert into avgagi (zipcode, avg_agi)
select zipcode, sum(A00100)/sum(N1) as 'avg_agi' from taxbyzip2008 group by zipcode;