我在从两个表中指定货币(价格)的SUM时遇到问题。结果错误的SUM结果问题。
我的查询:
SELECT SUM(up2.price + uf1.price) AS TotalCredit
FROM user_faktorha AS uf1, u_payment AS up2
WHERE uf1.`acc_id` = '11784' && up2.`Account`='11784' && up2.`Active`='1'
表格user_faktorha
列Price
包含正数和负数
表格u_payment
列Price
只有正数。
出了什么问题?
答案 0 :(得分:0)
试试这个
SELECT (up2.price + uf1.price) AS TotalCredit
FROM user_faktorha AS uf1, u_payment AS up2
WHERE uf1.`acc_id` = '11784' && up2.`Account`='11784' && up2.`Active`='1'
因为你会添加它们,你不需要SUM
答案 1 :(得分:0)
我可能会建议您稍微改变一下语法。以下是两种选择:
SELECT up2.price + uf1.price AS TotalCredit
FROM user_faktorha AS uf1
INNER JOIN u_payment AS up2
on uf1.acc_id = up2. Account
WHERE uf1.`acc_id` = '11784' && up2.`Active`='1';
或者
SELECT sum(up2.price + uf1.price) AS TotalCredit
FROM user_faktorha AS uf1
INNER JOIN u_payment AS up2
on uf1.acc_id = up2. Account
WHERE uf1.`acc_id` = '11784' && up2.`Active`='1'
GROUP BY uf1.acc_id
注意两个表之间使用的INNER JOIN
语法,它们现在加入acc_id
。同样在第一个版本中,我没有使用聚合函数,我只在两个表中添加price
,而第二个版本使用sum()
聚合并添加group by