MySQL SUM Cash Value按类别(JOIN)

时间:2019-03-16 11:21:19

标签: mysql

我想显示每个类别的支出数据。我有两个表格条纹,便笺和类别
笔记表:

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

1 个答案:

答案 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

相关问题