1

恐怕我已经写了一个查询并且在这个过程中让自己感到困惑,尽管它很简单。

我有 2 个 mysql 表。

Table1 has ... orderID, productID, quantity
Table2 has ... orderID, status, time

我需要做一个查询,它将执行以下操作......

Output (1 per line) 
productID - Quantity  where status = 1 & time < $lastactive. 

我尝试对表 2 进行查询以获取 productID 并计算 Quantity,但是如果 2 个不同的 orderID 具有相同的 productID,则它们不会总计。非常感谢任何帮助(表/行的名称是准确的)。

例子:

orderID 123, productID 2, quantity 4
orderID 123, productID 5, quantity 6
orderID 678, productID 2, quantity 5

会输出:

2   9
5   6
4

1 回答 1

1

您应该能够使用类似的东西:

select t1.productId, Sum(t1.quantity) Total
from table1 t1
inner join table2 t2
  on t1.orderid = t2.orderid
where t2.status = 1
  and t2.time < $lastactive
group by t1.productid
于 2012-11-26T19:38:31.693 回答