Php mysql子查询计数where和group by

Php mysql子查询计数where和group by,php,mysql,Php,Mysql,我有四张像这样的桌子 cc_代理 抄送电话 呼叫方要求 抄送通知 我执行的sql查询是 SELECT cca.id, cca.username, (SELECT COUNT(cccr.id) FROM cc_caller_requirements AS cccr WHERE cccr.cc_agent_id = cca.id GROUP BY cccr.cc_caller_id) AS num_of_callers, (SELECT COUNT(ccns

我有四张像这样的桌子

cc_代理 抄送电话 呼叫方要求 抄送通知 我执行的sql查询是

SELECT cca.id, cca.username,
   (SELECT COUNT(cccr.id) 
    FROM cc_caller_requirements AS cccr 
    WHERE cccr.cc_agent_id = cca.id 
    GROUP BY cccr.cc_caller_id) AS num_of_callers,
   (SELECT COUNT(ccns.id) 
    FROM cc_notifications AS ccns 
    WHERE ccns.cc_agent_id = cca.id  
    AND   ccns.cc_notification_type_id = 'sms') AS sms,
   (SELECT COUNT(ccnm.id) 
    FROM cc_notifications AS ccnm 
    WHERE ccnm.cc_agent_id = cca.id 
    AND   ccnm.cc_notification_type_id = 'mail') AS mail,
   (SELECT COUNT(ccna.id) 
    FROM cc_notifications AS ccna 
    WHERE ccna.cc_agent_id = cca.id 
    AND   ccna.cc_notification_type_id = 'courier') AS courier
FROM cc_agents AS cca
GROUP BY cca.id
我正在寻找这样的输出:

+------------+---------------+-----------+------------+---------------+
| agent name | no of callers | total sms | total mail | total courier |
+------------+---------------+-----------+------------+---------------+
| sankar     | 5             | 7         | 6          | 3             |
| jenifer    | 0             | 0         | 0          | 0             |
| andrew     | 0             | 0         | 0          | 0             |
| nirmal     | 1             | 1         | 1          | 1             |
| raja       | 1             | 2         | 2          | 2             |
+------------+---------------+-----------+------------+---------------+
代理名称,总短信,总邮件和总快递数据工作良好

但是,当我不需要任何调用方时,会出现“子查询返回超过1行”的错误

请帮我解决它

将您的查询更改为:

SELECT cca.id, cca.username,
   (SELECT COUNT(DISTINCT cccr.cc_caller_id) 
    FROM cc_caller_requirements AS cccr 
    WHERE cccr.cc_agent_id = cca.id 
    ) AS num_of_callers,
   (SELECT COUNT(ccns.id) 
    FROM cc_notifications AS ccns 
    WHERE ccns.cc_agent_id = cca.id  
    AND   ccns.cc_notification_type_id = 'sms') AS sms,
   (SELECT COUNT(ccnm.id) 
    FROM cc_notifications AS ccnm 
    WHERE ccnm.cc_agent_id = cca.id 
    AND   ccnm.cc_notification_type_id = 'mail') AS mail,
   (SELECT COUNT(ccna.id) 
    FROM cc_notifications AS ccna 
    WHERE ccna.cc_agent_id = cca.id 
    AND   ccna.cc_notification_type_id = 'courier') AS courier
FROM cc_agents AS cca
GROUP BY cca.id

非常感谢你。。。您的查询非常有效。。。谢谢lot@user3101664不,此查询是错误的,表甚至没有联接。运行时没有错误的查询不一定是有效的查询。@begueradj,您能按照预期的输出发布您的查询吗。。。thanks@Begueradj您认为它有什么问题,为什么要使用联接?
+----+-------------+--------------+-------------------+----------------------+
| id | cc_agent_id | cc_caller_id | cc_requirement_id | cc_notification_type |
+----+-------------+--------------+-------------------+----------------------+
| 1  | 1           | 5            | 1                 | sms                  |
| 2  | 1           | 5            | 1                 | mail                 |
| 3  | 1           | 5            | 1                 | courier              |
| 4  | 1           | 5            | 2                 | sms                  |
| 5  | 1           | 5            | 2                 | mail                 |
| 6  | 1           | 2            | 3                 | sms                  |
| 7  | 1           | 2            | 4                 | sms                  |
| 8  | 1           | 2            | 4                 | mail                 |
| 9  | 1           | 2            | 4                 | courier              |
| 10 | 1           | 7            | 5                 | mail                 |
| 11 | 1           | 7            | 5                 | courier              |
| 12 | 4           | 2            | 6                 | sms                  |
| 13 | 4           | 2            | 6                 | mail                 |
| 14 | 4           | 2            | 6                 | courier              |
| 30 | 5           | 2            | 12                | sms                  |
| 31 | 5           | 2            | 12                | mail                 |
| 32 | 5           | 2            | 12                | courier              |
| 33 | 5           | 2            | 13                | sms                  |
| 34 | 5           | 2            | 13                | mail                 |
| 35 | 5           | 2            | 13                | courier              |
| 36 | 1           | 2            | 14                | sms                  |
| 37 | 1           | 8            | 15                | sms                  |
| 38 | 1           | 8            | 15                | mail                 |
| 39 | 1           | 9            | 16                | sms                  |
| 40 | 1           | 9            | 16                | mail                 |
+----+-------------+--------------+-------------------+----------------------+
SELECT cca.id, cca.username,
   (SELECT COUNT(cccr.id) 
    FROM cc_caller_requirements AS cccr 
    WHERE cccr.cc_agent_id = cca.id 
    GROUP BY cccr.cc_caller_id) AS num_of_callers,
   (SELECT COUNT(ccns.id) 
    FROM cc_notifications AS ccns 
    WHERE ccns.cc_agent_id = cca.id  
    AND   ccns.cc_notification_type_id = 'sms') AS sms,
   (SELECT COUNT(ccnm.id) 
    FROM cc_notifications AS ccnm 
    WHERE ccnm.cc_agent_id = cca.id 
    AND   ccnm.cc_notification_type_id = 'mail') AS mail,
   (SELECT COUNT(ccna.id) 
    FROM cc_notifications AS ccna 
    WHERE ccna.cc_agent_id = cca.id 
    AND   ccna.cc_notification_type_id = 'courier') AS courier
FROM cc_agents AS cca
GROUP BY cca.id
+------------+---------------+-----------+------------+---------------+
| agent name | no of callers | total sms | total mail | total courier |
+------------+---------------+-----------+------------+---------------+
| sankar     | 5             | 7         | 6          | 3             |
| jenifer    | 0             | 0         | 0          | 0             |
| andrew     | 0             | 0         | 0          | 0             |
| nirmal     | 1             | 1         | 1          | 1             |
| raja       | 1             | 2         | 2          | 2             |
+------------+---------------+-----------+------------+---------------+
SELECT cca.id, cca.username,
   (SELECT COUNT(DISTINCT cccr.cc_caller_id) 
    FROM cc_caller_requirements AS cccr 
    WHERE cccr.cc_agent_id = cca.id 
    ) AS num_of_callers,
   (SELECT COUNT(ccns.id) 
    FROM cc_notifications AS ccns 
    WHERE ccns.cc_agent_id = cca.id  
    AND   ccns.cc_notification_type_id = 'sms') AS sms,
   (SELECT COUNT(ccnm.id) 
    FROM cc_notifications AS ccnm 
    WHERE ccnm.cc_agent_id = cca.id 
    AND   ccnm.cc_notification_type_id = 'mail') AS mail,
   (SELECT COUNT(ccna.id) 
    FROM cc_notifications AS ccna 
    WHERE ccna.cc_agent_id = cca.id 
    AND   ccna.cc_notification_type_id = 'courier') AS courier
FROM cc_agents AS cca
GROUP BY cca.id