0

谁能帮我解决mysql查询:-

select 
  (select 
     count(bug_id) 
   from 
     bugs 
   where 
     bugs.priority="P3") as P3count,
  (select count(bug_id) from bugs where bugs.priority="P2") as P2count 
from 
  bugs 
where 
  bugs.product_id=237 and 
  bugs.bug_status='RESOLVED' and 
  bugs.resolution='FIXED' and 
  bugs.creation_ts >= '2013-06-14 09:00:00' and 
  bugs.creation_ts <= '2013-06-16 08:59:59' 
group by 
  priority;

需要得到结果:-

+---------+----------+
| P3count | P2count |                                                                                                                                          
+---------+----------+                                                                                                                                          
| 7 | 8 |                                                                                                                                      
+---------+----------+
4

1 回答 1

1

而不是使用COUNT,您可以SUM通过添加1每个匹配的行来计算行数。表达式IF(condition,1,0)计算1条件是否为真,0否则计算。

SELECT
    SUM(IF(priority="P3",1,0)) P3count,
    SUM(IF(priority="P2",1,0)) P2count,
    ...
FROM bugs
WHERE ...
于 2013-06-15T08:53:51.207 回答