恐怕我写了一个查询并在这个过程中弄糊涂了,尽管它很简单。
我有 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 并计算数量,但如果 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
最佳答案
你应该能够使用类似这样的东西:
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
关于php - 具有适当计数的双重查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13571807/