gpt4 book ai didi

MySQL SUM 现金值(value)按类别 (JOIN)

转载 作者:行者123 更新时间:2023-11-29 09:48:52 26 4
gpt4 key购买 nike

我想显示每个类别的支出数据。我有两个表格条纹,注释和类别
注释表:

id | title | amount | type | cID
1 | Some ... | 50 | out | 1
2 | Some ... | 25 | out | 1
3 | Some ... | 20 | out | 2
4 | Some ... | 75 | out | 1
5 | Some ... | 50 | out | 2

类别表:

id | cID | cName
1 | 1 | Home
2 | 2 | School

我想这样显示:
家庭:150 美元
学校:70美元

我的查询:

SELECT SUM(amount) from notes n Join category c On n.cID = c.CID

最佳答案

您需要按类别名称分组:

SELECT concat(c.cname,  ': $', SUM(n.amount)) totals
from notes n Join category c On n.cID = c.CID
GROUP BY c.cname

请参阅demo

关于MySQL SUM 现金值(value)按类别 (JOIN),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55196169/

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