MySQL:SUM() 功能不起作用

标签 mysql sql sum

我试图对通过将其他两列相乘而创建的列求和,但它似乎不起作用。每次尝试输入 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/

相关文章:

sql - 主键在非聚集索引中可用吗?

excel - 在一列中求和数字

mysql - 选择表 1 和表 2 的计数

mysql - 查找并替换mysql中的时间戳数据

sql - 欧洲网站的最佳字符集和排序规则

sql - 如何编写一个简单的数据库引擎

mysql - 无法添加外键约束为主外键

python - 查找列表中列的总和获取 "TypeError: cannot perform reduce with flexible type"

python - 索引对之间子数组中值的 Numpy 总和

mysql - 优化 AWS EC2 t2.small 的 MySQL 设置