将查询结果插入表中

时间:2015-02-17 08:02:35

标签: php mysql

我在这里

 table_A
| Customer ID | moneyspent |
    001             50
    002             30
    003             20
    003             20
    002             30

我看过一个查询,它从表A中得到所有moneyspent的总和

SELECT SUM(moneyspent) FROM table_A

但我希望将结果插入表B的列“totalspent”中,如此

 table_B
| Customer ID | totalspent |
   001           50
   002           60
   003           40

帮忙。 感谢

1 个答案:

答案 0 :(得分:1)

试试这个,它的确有效:

INSERT INTO table_B (Customer_ID, totalspent) 
(SELECT Customer_ID, sum(moneyspent) FROM table_A group by Customer_ID)