我想显示每个类别的支出数据。我有两个表格条纹,便笺和类别
笔记表:
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
我的查询:
从注释n中选择SUM(金额) 加入类别c 在n.cID = c.CID
答案 0 :(得分:1)
您需要按类别名称分组:
SELECT concat(c.cname, ': $', SUM(n.amount)) totals
from notes n Join category c On n.cID = c.CID
GROUP BY c.cname
请参见demo