如何在join语句中汇总一列?

时间:2019-07-11 20:40:08

标签: mysql join

我正在尝试连接多个表,并且我需要返回某些列的总和。我已经尝试过使用SUM,但它的行为却与我期望的JOIN语句不同。

我的表设置如下:

daily_balances
+----------------+-------------+-------------+
| account_number | balance_date| total_value |
+----------------+-------------+-------------+
|    1           | 01/01/2018  |  100.00     |
|    1           | 01/17/2018  |  250.00     |
|    1           | 02/22/2018  |  175.00     |

account_inflows
+----------------+-------------+-------------+
| account_number | inflow_date | total_value |
+----------------+-------------+-------------+
|    1           | 01/01/2018  |  100.00     |
|    1           | 01/01/2018  |   50.00     |
|    1           | 01/17/2018  |   75.00     |

account_outflows
+----------------+-------------+-------------+
| account_number | inflow_date | total_value |
+----------------+-------------+-------------+
|    1           | 02/22/2018  |  100.00     |

运行以下语句:

select `daily_balances`.`balance_date` as `date`, `daily_balances`.`total_value` as `ending_value`, `account_inflows`.`total_value` as `account_inflow`, `account_outflows`.`total_value` as `account_outflow`, 
from `daily_balances` 
left join `account_inflows` on `daily_balances`.`account_number` = `account_inflows`.`account_number` and `daily_balances`.`balance_date` = `account_inflows`.`inflow_date` 
left join `account_outflows` on `daily_balances`.`account_number` = `account_outflows`.`account_number` and `daily_balances`.`balance_date` = `account_outflows`.`outflow_date` 
where `daily_balances`.`account_number` = 1 and `daily_balances`.`balance_date` = '2018-01-01'

返回:

+------------+--------------+----------------+-----------------+
| date       | ending_value | account_inflow | account_outflow |
+------------+--------------+----------------+-----------------+
| 01/01/2018 | 100.00       |     100.00     |       null      |
| 01/01/2018 | 100.00       |      50.00     |       null      |
| 01/17/2018 | 250.00       |      75.00     |       null      |
| 02/22/2018 | 175.00       |       null     |      75.00      |

相反,我想对account_inflow列求和:

+------------+--------------+----------------+-----------------+
| date       | ending_value | account_inflow | account_outflow |
+------------+--------------+----------------+-----------------+
| 01/01/2018 | 100.00       |     150.00     |       null      |
| 01/17/2018 | 250.00       |      75.00     |       null      |
| 02/22/2018 | 175.00       |       null     |      75.00      |

1 个答案:

答案 0 :(得分:0)

您当前的查询仅返回单个行,但不执行任何操作以实际累加列。

select `daily_balances`.`balance_date` as `date`, 
    `daily_balances`.`total_value` as `ending_value`, 
    Sum(`account_inflows`.`total_value`) as `account_inflow`, 
    `account_outflows`.`total_value` as `account_outflow`
from `daily_balances` 
left join `account_inflows` on `daily_balances`.`account_number` = `account_inflows`.`account_number` and  
    `daily_balances`.`balance_date` = `account_inflows`.`inflow_date` 
left join `account_outflows` on `daily_balances`.`account_number` = `account_outflows`.`account_number` and 
    `daily_balances`.`balance_date` = `account_outflows`.`outflow_date` 
where `daily_balances`.`account_number` = 1 and 
    `daily_balances`.`balance_date` = '2018-01-01'
Group By `daily_balances`.`balance_date`

由于您不想将所有值求和,而只是在不同的日期求和,因此需要 Group By 子句。自动取款机,我没有办法验证这是做您想要做的完全正确的查询,但是它应该使您朝着正确的方向前进。

您可以在https://www.w3resource.com/sql/aggregate-functions/sum-with-group-by.php

上了解有关此内容的更多信息。

此外,您应该做更多的事情来格式化查询,以使其更具可读性。代码清晰几乎与拥有正确的代码一样重要,因为使其具有可读性将使其得以修正或只是重新阅读而已,而这几乎不需要进行脑力劳动。我已经将其格式化为我的教学方法,但是还有其他方法可以研究。