在mysql中填充采购订单表

时间:2017-02-19 09:42:17

标签: mysql

我在mysql中有两个表order_headerorder_details。第一个表order_header包含order_idorder_amountorder_date。如下所示:

    +----------+--------+------------+
    | order_id |amount  | order_date |
    +----------+--------+------------+
    |  2001    |  700   |01-02-2017  |
    |  2002    |  1200  |02-02-2017  |
    +----------+--------+------------+

第二张表order_details包含order_iditem_namequantityamountorder_date。如下所示:

    +----------+---------------+---------+--------+------------+
    | order_id | item_name     |quantity |amount  | order_date |
    +----------+---------------+---------+--------+------------+
    |  2001    |Chocolate Cake |    2    |  500   |01-02-2017  |
    |  2001    |Bevarages      |    4    |  200   |01-02-2017  |
    |  2002    |Strawberry Cake|    2    |  600   |02-02-2017  |
    |  2002    |Bevarages      |    12   |  600   |02-02-2017  |
    +----------+---------------+---------+--------+------------+    

我的问题是当order_detailsorder_header表中唯一时,如何将order_id的数据填充到order_header表中。如何在order_id表中为一堆订单生成序列中的order_details

1 个答案:

答案 0 :(得分:0)

以下查询可以帮助您

insert into order_header(order_id, amount, order_date)
select order_id, sum(amount), order_date
from order_details
group by order_id, order_date