【发布时间】:2018-02-17 17:58:56
【问题描述】:
我有两张桌子:
第一:原因
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表。你的意思是left join projects?