简体   繁体   中英

MySQL COUNT from multiple tables with group

I'm looking to find 2 results from the disciplines table below. The first COUNT would be to see how many Certificates are assigned to each discipline by using the disciplines_certificates table. The second COUNT would be to see how many Workers have the discipline assigned workers_disciplines

I have managed to do individual queries to fetch these answers, but I'm not sure what I need to do in order to get both results in 1 query. (I've just copied over the results with an actual answer to save space, but the end result should include all disciplines.

Query to select the used certificates

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`

Result:

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

Query to select the used disciplines

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`

Result:

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

Query I tried to use to select all data:

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`

Expected Result:

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

Actual Result:

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

You can find the SQLfiddle here: http://sqlfiddle.com/#!9/392c4/3

Table disciplines

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

Table disciplines_certificates

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

Table workers_disciplines

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

Thanks.

Use the DISTINCT statement to count only distinct ids.

    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`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM