-2

我在从两个表中指定货币(价格)的总和时遇到问题。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_faktorhaPrice有正数和负数。
u_paymentPrice只有正数。

有什么问题或问题?

4

2 回答 2

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

于 2012-12-23T19:10:09.407 回答
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

于 2012-12-23T19:30:49.673 回答