我有两个表格,其中包含以下条目:
表1 tblcategories
id类别
1建筑师
2工程师
3项目经理
表2 tblpayments
id / categoryid / payment
1/1/100
2/2/150
3/2/50
4/1/200
5/1/50
当前我的记录集sql看起来像这样:
SELECT tblcategories.id,tblcategories.category,SUM(tblpayments.payment)AS total 来自tblcategories,tblpayments WHERE(tblcategories.id = tblpayments.category) GROUP BY tblcategories.id
这给了我以下结果:
记录/标识/类别/总计
1/1 / Architect / 350
2/2 / Engineer / 200
但是,我想得到以下结果:
记录/标识/类别/总计
1/1 / Architect / 350
2/2 / Engineer / 200
3/3 /项目经理/ 0
这可能吗?
答案 0 :(得分:1)
试试这个
SELECT cat.id, cat.category, SUM(pay.payment) AS total
FROM tblcategories cat LEFT JOIN tblpayments pay
ON cat.id = pay.category
GROUP BY cat.id