我有两个具有一对多关系的表(orders、order_lines)。orders 表将有一行,而 order_lines 表将有一行用于附加到订单的每个产品。
订单
+----------+-------------+---------------+
|order_num | total_order | shipping_total|
+----------+-------------+---------------+
| 12345 | 75.00 | 15.00 |
+----------+-------------+---------------+
ORDER_LINES
+----------+-------------+-------+
| order_num| volume | price |
+----------+-------------+-------+
| 12345 |10 |25.00 |
+----------+-------------+-------+
| 12345 |20 |25.00 |
+----------+-------------+-------+
| 12345 |20 |25.00 |
+----------+-------------+-------+
我想总结 ORDERS 表中的 total_order 和 shipping_total 列以及 ORDER_LINES 表中的量和价格列。
查询当前不起作用:
SELECT b_orders.bill_country, b_orders.ship_country
, SUM(volume) AS v, SUM(price) AS pt
, SUM(shipping_total) AS st, SUM(total_order) AS tot_o
FROM b_orders
JOIN b_order_lines
ON b_orders.order_num = b_order_lines.order_num
WHERE DATE(b_orders.order_date) BETWEEN '2012-04-02' AND '2012-04-06'
AND ship_country = 'USA'
AND b_order_lines.price > 0;
此查询返回错误的值。我有点知道为什么,但不知道如何编写正确的查询...请帮助。