为什么sql中两列相乘不等于sum(column1 * column2)

问题描述 投票:0回答:1

如果有这些表格:

订单(金额,单价,子金额,子价格,service_id,发行日期,是否成功)

池(名称,for_view)

服务(id,pool_id)

当我运行这个sql时:

SELECT SUM(orders.amount) AS totalAmount, 
SUM(orders.unit_price) AS totalPrice,  
SUM(orders.amount*orders.unit_price) AS total,  
SUM(orders.child_amount) AS totalAmountC,  
SUM(orders.child_price) AS totalPriceC,  
SUM(orders.child_amount*orders.child_price) AS totalC,  
pools.name, 
pools.for_view 
FROM orders INNER 
JOIN services ON services.id = orders.service_id  
INNER JOIN pools ON pools.id = services.pool_id 
WHERE issue_date = '2024-03-15' 
AND pools.id = '35' 
AND is_success = 1  
GROUP BY pools.name, pools.for_view; 

总金额 * 总价格不等于总金额!!!!!!

当我运行 sql 时,查看 phpmyadmin 中的结果:

为什么这两列不等于?总数应该是 5.000.000 但它是错误的!我该如何修复它?

sql join phpmyadmin sum
1个回答
0
投票

因为乘积之和不等于和的乘积。通过一个简单的例子就可以很容易地看出这一点:

金额 单价 数量*单价
2 10 20
3 20 60
总计 80
金额 单价总和 总计
5 30 150
© www.soinside.com 2019 - 2024. All rights reserved.