我在mysql中有两个表order_header
和order_details
。第一个表order_header
包含order_id
,order_amount
,order_date
。如下所示:
+----------+--------+------------+
| order_id |amount | order_date |
+----------+--------+------------+
| 2001 | 700 |01-02-2017 |
| 2002 | 1200 |02-02-2017 |
+----------+--------+------------+
第二张表order_details
包含order_id
,item_name
,quantity
,amount
,order_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_details
在order_header
表中唯一时,如何将order_id
的数据填充到order_header
表中。如何在order_id
表中为一堆订单生成序列中的order_details
?
答案 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