gpt4 book ai didi

mysql - 我可以在 mysql 中创建 View 来汇总基于另一列的所有列吗?

转载 作者:行者123 更新时间:2023-11-29 19:03:07 26 4
gpt4 key购买 nike

我有一个包含帐户类型金额的表格只有两种类型的账户(储蓄和信贷):enter image description here

我创建了一个 View ,该 View 将根据帐户类型对所有金额总和进行分组。

表格可能如下所示

enter image description here

最佳答案

使用条件聚合

create view your_view_name as 
select id,
sum(case when account = 'savings' then ammount else 0 end) as saving,
sum(case when account = 'credit' then ammount else 0 end) as credit
from your_table
group by id

关于mysql - 我可以在 mysql 中创建 View 来汇总基于另一列的所有列吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43707708/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com