通过对SQL查询进行分组来检索单个计数

时间:2017-01-25 18:24:10

标签: mysql select count group-by

我想检索待处理查询的计数和每位顾问的已完成查询的计数,并显示在表格中。检索这些数据的SQL查询是什么?

SELECT CounsellorName, 
  (SELECT COUNT(Status) 
   FROM register WHERE Status='Pending') AS pending, 
  (SELECT COUNT(Status) FROM register WHERE Status='Completed') AS completed
FROM register  
GROUP BY CounsellorName

我目前正在使用此功能,但它不会检索每位顾问的个人计数。

感谢您的支持!

1 个答案:

答案 0 :(得分:0)

试试这个:

SELECT CounsellorName, 
  sum(Status='Pending') AS pending, 
  sum(Status='Completed') AS completed
FROM register  
GROUP BY CounsellorName

它根据counsellorName查找待处理和已完成状态的计数。