我有一个包含订购商品的表格,偶尔我有一个订单ID,包含多行(每个订购商品一行)。
OrderID | Order.Item_id | Item.Price | Quantity | OrderTotal | Seller ID |
1 | 1 | 10 | 1 | 10 |1 |
2 | 2 | 15 | 1 | 25 |1 |
2 | 3 | 10 | 1 | 25 |2 |
3 | 4 | 15 | 1 | 44 |1 |
3 | 5 | 10 | 1 | 44 |1 |
3 | 6 | 19 | 1 | 44 |2 |
我需要一种方法来导出/显示给定卖家ID的OrderTotal。
理想情况下,我需要以下行的结果:
OrderID | Order.Item_id | Item.Price | Quantity | OrderTotal | Seller ID |
1 | 1 | 10 | 1 | 10 |1 |
2 | 2 | 15 | 1 | 15 |1 |
3 | 4 | 15 | 1 | 25 |1 |
3 | 5 | 10 | 1 | 25 |1 |
对于OrderID 2,我可以轻松选择项目价格,但这对于OrderID 3不起作用,我需要将两个项目相加。这只能使用SQL吗?你能帮我吗?
非常感谢提前!
答案 0 :(得分:0)
尝试如下
Select SellerID,
OrderID,
-- below will display the items id comma seperated
group_concat(Itme_id) as itemlist,
-- below will display the item price comma seperated, you can use sum(Price) also
group_concat(Price) as pricelist,
sum(quantity) as totalquantity,
sum(OrderTotal) as totalorders
From Table
where SellerID = 1
group by SellerID,OrderID
答案 1 :(得分:0)
您可以按订单和卖家获取订单总和,然后加入订单信息获取其他值:
select o.order_id, o.item_id, o.price, o.quantity, q.total, o.seller_id from (
select order_id, seller_id, sum(price) total
from orders
where seller_id = 1
group by order_id, seller_id
) q
join orders o on (o.order_id = q.order_id and q.seller_id = o.seller_id);