1
的百分比我有两个表:我需要计算计数的项目
1:原因
id | title
---------------------------------
1 | Customer didn't like it
2 | Needs improving
3 | Wrong format
第二:项目
id | title | rejected
------------------------------------
1 | Priject 1 | Null
2 | Priject 2 | 1
3 | Priject 3 | 1
4 | Priject 4 | Null
5 | Priject 5 | 2
我需要显示Reasons.Title和数量项目因此被拒绝。我已经成功地加入这些表一起,与此代码
SELECT reasons.title as title, count(*) as num
FROM reasons
LEFT JOIN reasons on projects.rejected = reasons.id
WHERE projects.rejectedIS NOT NULL
GROUP BY projects.rejected
现在我需要添加比例,所以我最后的表看起来像这样
title | num | percentage
--------------------------------------------------
Customer didn't like it | 2 | 66,6
Needs improving | 1 | 33,3
百分比的格式,当然并不重要。 我希望得到这个与MySQL完成的,所以我并不需要使用两个查询和额外的PHP,但如果有另一种解决方案,其他从MySQL,我愿意听听建议
您没有加入'projects'表。你是不是指“左连接项目”? – Barmar