想要同时统计bug表的激活bug数量,与已完成bug数量。
SELECT p.name
‘所属项目’, COUNT(b.status
) ‘激活状态BUG总数’
FROM zt_bug b ,zt_project p WHERE b.project
IN (464,465,466) AND p.id
=b.project
AND b.status
=’active’
GROUP BY project;
SELECT p.name
‘所属项目’, COUNT(b.status
) ‘已解决状态BUG总数’
FROM zt_bug b ,zt_project p WHERE b.project
IN (464,465,466) AND p.id
=b.project
AND b.status
=’resolved’
GROUP BY project;
失败的尝试:
SELECT p.name
‘所属项目’,COUNT(b.status
= ‘active’) ‘激活状态安全BUG总数’,COUNT(b.status
= ‘resolved’) ‘已解决状态安全BUG总数’
FROM zt_bug b,zt_project p
WHERE b.project
IN (464, 465, 466)
AND p.id
= b.project
GROUP BY project ;
解决方法:
mysql提供if函数,可以在查询是使用。
SELECT p.name
‘所属项目’,
SUM(
IF((b.status
=’active’),1,0)
) ‘激活状态安全BUG总数’,
SUM(
IF((b.status
=’resolved’),1,0)
) ‘已解决状态安全BUG总数’
FROM zt_bug b ,zt_project p WHERE b.project
IN (464,465,466) AND p.id
=b.project
GROUP BY project;