Mysql group by和having&表1中一列的总和

时间:2016-04-01 11:17:41

标签: mysql sql database

目前我正在运行一个sql脚本,该脚本通过以下查询提供两个表(cc_transactions和cc_transaction_status)的结果:

SELECT cc_transactions.transAmount as amt
FROM cc_transactions 
INNER JOIN cc_transaction_status ON cc_transactions.id = cc_transaction_status.transaction_id 
WHERE account_id = '11E5F26C8164C3BABF85F8B156B0DBF5'
GROUP BY cc_transactions.id 
HAVING MAX(cc_transaction_status.sortOrder) = 0

结果是

amt     
200.0000
300.0000
1000.0000

我希望结果是

amt     
1500.0000

任何人都可以参与其中。

1 个答案:

答案 0 :(得分:1)

SELECT SUM(Amount.amt) from (
      SELECT cc_transactions.transAmount as amt
      FROM cc_transactions 
      INNER JOIN cc_transaction_status ON cc_transactions.id = cc_transaction_status.transaction_id 
      WHERE account_id = '11E5F26C8164C3BABF85F8B156B0DBF5'
      GROUP BY cc_transactions.id 
      HAVING MAX(cc_transaction_status.sortOrder) = 0
) as Amount;

如果您的查询成功运行,那么它将给予总和....