-1

表:订购产品

orderid   quantity       weight
-----------------------------------
4           5             0.1
4           2             0.5
5           3             2.5
5           7             0.9

1) 使用以下 SQL 命令,可以正确计算总重量,但忽略产品数量。如何扩展我的 SQL 命令来查询产品数量?

select orderid, sum (weight) as totalweight from orderproducts group by orderid

现在我有 ORDERID 4 的值(总重量)=> 0.6

必须正确 => 1.5

2)另外,我希望SQL命令“WHERE”(其中other_table.status =“finish”)从另一个表扩展。它也不起作用。:-(

select orderid, sum (weight) as totalweight from orderproducts where other_table.status = "finish" group by orderid
4

3 回答 3

2

询问:

SELECT orderid,
       SUM(weight*quantity) AS totalweight
FROM orderproducts AS a
JOIN othertable AS b ON (b.orderid=b=id)
WHERE b.status = "finish"
GROUP BY orderid
于 2013-09-04T05:34:49.443 回答
0

你可以试试这个:

1)产品数量:

select orderid,sum (amount) as totalamount, sum (weight) as totalweight 
   from orderproducts group by orderid

2)除非您在其中放置子查询,否则您不能将其他表放入当前查询中表的 where 子句中。试试这个:

select orderid, sum (weight) as totalweight from orderproducts where orderid in 
   (select orderid from other_table where other_table .status = "finish")
group by orderid
于 2013-09-04T04:53:30.410 回答
0

1)

select orderid,sum (amount) as totalamount, sum (weight) as totalweight from orderproducts group by orderid

2)你必须使用加入

  select orderid, sum (weight*quantity) as totalweight from orderproducts as a join othertable AS b on (b.orderid=b=id) where b.status = "finish" group by orderid
于 2013-09-04T04:54:18.320 回答