简体   繁体   English

Mysql group by 和表一中一列的总和

[英]Mysql group by and having & sum of one column from table one

Currently I am running a sql script that gives result from two tables (cc_transactions and cc_transaction_status) with below query:目前我正在运行一个 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

The result is结果是

amt     
200.0000
300.0000
1000.0000

I want the result to be我希望结果是

amt     
1500.0000

Can anyone please look in to this.任何人都可以看看这个。

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;

If your query run successfully then it will give sum....如果您的查询运行成功,那么它会给出 sum....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM