2017-02-19 118 views
0

我在mysql中有兩個表order_headerorder_details。第一個表order_header包含order_idorder_amount,order_date。如下所示:填充mysql中的採購訂單表

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

和具有order_iditem_namequantityamountorder_date第二表order_details。如下圖所示:

+----------+---------------+---------+--------+------------+ 
    | 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_details的數據填充到order_header表時order_idorder_header表獨一無二的。我怎樣才能在order_details表中的一堆訂單序列中生成order_id

回答

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