简体   繁体   English

如何对多列求和?

[英]How to sum multiple columns?

my question is: is it possible to sum multiple columns and get the total of every column?我的问题是:是否可以对多列求和并得到每列的总和? I'm using this query:我正在使用这个查询:

SELECT borrower, SUM(collateral_amount) AS collateral_amount, SUM(withdraw_amount) AS withdraw_amount, SUM(total_amount_to_treasure) as total_amount_to_treasure, SUM(id) as id 
FROM loans 
GROUP BY borrower

But that result is not what I want.但那个结果不是我想要的。 What I would like to get is for example collateral_amount: 30000 (second image)我想得到的是例如 collateral_amount: 30000 (second image)

第一张图片

第二张图

And I would like to get this as a result:结果我想得到这个:

{  
"collateral_amount": "600000", //The total sum of the collateral_amount column  
"id": "10", //The total sum of the id column  
"withdraw_collateral": "30000", //The total sum of tthe withdraw_collateral column  
"total_amount": "90000" //The total sum of the total_amount  
}

If the task to sum all columns then you shouldn't use group by :如果要对所有列求和,则不应使用group by

SELECT SUM(collateral_amount) AS collateral_amount,
   SUM(withdraw_amount) AS withdraw_amount,
   SUM(total_amount_to_treasure) AS total_amount_to_treasure,
   SUM(id) AS id
FROM loans;

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

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