我试图对通过将其他两列相乘而创建的列求和,但它似乎不起作用。每次尝试输入 SUM(PriceTimesQuantity)
任何和所有的帮助都会很棒!
这是我的查询:
select
OrderedProduct.orderId,
CustOrder.customerId,
CustOrder.orderDate,
OrderedProduct.paidPrice * OrderedProduct.qtyOrdered as PriceTimesQuantity
from OrderedProduct
join CustOrder
on CustOrder.orderId=OrderedProduct.orderId
where orderDate between '2014-01-01' and '2014-12-31'
group by orderDate
最佳答案
您应该只在 select
中包含 group by
或聚合函数中的列。因此,没有诸如 orderId
之类的列。
这可能更接近您想要做的事情:
select co.orderDate,
sum(op.paidPrice * op.qtyOrdered) as PriceTimesQuantity
from OrderedProduct op join
CustOrder co
on co.orderId = op.orderId
where co.orderDate between '2014-01-01' and '2014-12-31'
group by co.orderDate;
您的具体错误是您无法引用同一 select
子句中定义的别名。
关于MySQL:SUM() 功能不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33597810/