来自具有组的多个表的MySQL COUNT

时间:2015-03-09 18:21:18

标签: mysql

我希望从下面的disciplines表中找到2个结果。第一个COUNT是使用disciplines_certificates表查看为每个学科分配了多少个证书。第二个COUNT是查看有多少工人分配了workers_disciplines

我已设法进行单独查询以获取这些答案,但我不确定我需要做什么才能在1个查询中获得两个结果。 (我刚刚用实际答案复制了结果以节省空间,但最终结果应该包括所有学科。

查询以选择使用的证书

SELECT `disciplines`.* , COUNT(disciplines_certificates.certificate_id) as used_certificates
FROM (`disciplines`)
LEFT JOIN `disciplines_certificates` ON `disciplines_certificates`.`discipline_id` = `disciplines`.`id`
GROUP BY `disciplines`.`id`

结果:

+----+-------------------------+-------------------+
| id |         discipline_name | used_certificates |
+----+-------------------------+-------------------+
| 10 |        Crane Op Level 3 |                 3 |
| 18 |        Appointed Person |                 2 |
+----+-------------------------+-------------------+

查询以选择使用的学科

SELECT `disciplines`.*, COUNT(workers_disciplines.discipline_id) as used_disciplines
FROM (`disciplines`)
LEFT JOIN `workers_disciplines` ON `workers_disciplines`.`discipline_id` = `disciplines`.`id`
GROUP BY `disciplines`.`id`

结果:

+----+-------------------------+------------------+
| id |         discipline_name | used_disciplines |
+----+-------------------------+------------------+
| 10 |        Crane Op Level 3 |                1 |
+----+-------------------------+------------------+

查询我试图用来选择所有数据:

SELECT `disciplines`.*, COUNT(disciplines_certificates.certificate_id) as used_certificates, COUNT(workers_disciplines.discipline_id) as used_disciplines
FROM (`disciplines`)
LEFT JOIN `disciplines_certificates` ON `disciplines_certificates`.`discipline_id` = `disciplines`.`id`
LEFT JOIN `workers_disciplines` ON `workers_disciplines`.`discipline_id` = `disciplines`.`id`
GROUP BY `disciplines`.`id`

预期结果:

+----+-------------------------+-------------------+------------------+
| id |         discipline_name | used_certificates | used_disciplines |
+----+-------------------------+-------------------+------------------+
| 10 |        Crane Op Level 3 |                 3 |                1 |
| 18 |        Appointed Person |                 2 |                0 |
+----+-------------------------+-------------------+------------------+

实际结果:

+----+-------------------------+-------------------+------------------+
| id |         discipline_name | used_certificates | used_disciplines |
+----+-------------------------+-------------------+------------------+
| 10 |        Crane Op Level 3 |                 3 |                3 |
| 18 |        Appointed Person |                 2 |                0 |
+----+-------------------------+-------------------+------------------+

您可以在此处找到SQLfiddle:http://sqlfiddle.com/#!9/392c4/3

disciplines

+----+-------------------------+
| id |         discipline_name |
+----+-------------------------+
|  1 |              Pipefitter |
|  2 |         Inst Pipefitter |
|  3 |                  Plater |
| 10 |        Crane Op Level 3 |
| 18 |        Appointed Person |
+----+-------------------------+

disciplines_certificates

+---------------+----------------+
| discipline_id | certificate_id |
+---------------+----------------+
|            10 |              6 |
|            10 |             15 |
|            10 |             20 |
|            18 |              6 |
|            18 |             15 |
+---------------+----------------+

workers_disciplines

+-----------|---------------+
| worker_id | discipline_id |
+-----------|---------------+
|         1 |            10 |
+-----------|---------------+

感谢。

1 个答案:

答案 0 :(得分:1)

使用DISTINCT语句仅计算不同的ID。

    SELECT `disciplines`.*, 
           COUNT(DISTINCT disciplines_certificates.certificate_id) AS used_certificates, 
           COUNT(DISTINCT workers_disciplines.discipline_id) AS used_disciplines
      FROM `disciplines`
 LEFT JOIN `disciplines_certificates` ON `disciplines_certificates`.`discipline_id` = `disciplines`.`id`
 LEFT JOIN `workers_disciplines` ON `workers_disciplines`.`discipline_id` = `disciplines`.`id`
  GROUP BY `disciplines`.`id`