我有两张桌子
订单
- ID
- 日期
- 供应商
项
- ID
- 订单ID
- 价格
- 数量
我正在尝试显示订单中所有商品的总数。像这样......
相反,我的结果显示如下..
所以它给了我订单中每件商品的总数,但不是订单中所有商品的总数,这是我所追求的。这是我当前的查询。
SELECT o.id AS oid, o.date AS date, (i.price*i.qty) AS subtotal FROM orders o, items i WHERE o.cosid=? AND o.id=i.OrderID GROUP BY o.id ORDER BY o.id DESC LIMIT 5;
答案 0 :(得分:1)
SELECT o.id AS oid, o.date AS date, SUM(i.price*i.qty) AS total_per_order_date
FROM orders o
INNER JOIN items i
ON o.id = i.orderid
GROUP BY o.id,o.date
ORDER BY o.id,o.date ;
答案 1 :(得分:0)
您需要将日期添加到GROUP BY
子句中,并应用SUM()
函数对价格求和:
SELECT o.id AS oid,
o.date AS date,
SUM(i.price*i.qty) AS subtotal
FROM orders o, items i
WHERE o.cosid=?
AND o.id=i.OrderID
GROUP BY o.id,o.date
ORDER BY o.id
DESC LIMIT 5
答案 2 :(得分:0)
尝试使用
sum(i.price*i.qty) as subtotal
否则你只是计算每个项目的结果。