Jump to content

MySQL COUNT from multiple tables in single query


thara

Recommended Posts

In my DB schema, there are 4 tables and its relationship as shown in the below attached image.

school.png.a40bca91f098115792b66c871994a684.png

My question is how many schools are there according to the data in the school table and how many of those schools belong to provinces, districts and zones, is it possible in one mysql query?

Currently I am using 4 separate queries for this and the relevant code is as follows.
 

$sql = "SELECT count(school_id) as schtot FROM sn_school";
$stmt = $pdo->query($sql);
$schtot = $stmt->fetchColumn();
$schtot = str_pad($schtot , 3, 0, STR_PAD_LEFT);

$sql = "SELECT count(zone_id) as zonetot FROM sn_school GROUP BY zone_id";
$stmt = $pdo->query($sql);
$zonetot = $stmt->rowCount();
$zonetot = str_pad($zonetot , 3, 0, STR_PAD_LEFT);

$sql = "SELECT COUNT(d.district_id) as districtTot
          FROM sn_school s
            JOIN zone z USING(zone_id)
            JOIN district d ON d.district_id = z.district_id
          GROUP BY d.district_id";
$stmt = $pdo->query($sql);
$districtTot = $stmt->rowCount();
$districtTot = str_pad($districtTot , 3, 0, STR_PAD_LEFT);

$sql = "SELECT COUNT(p.province_id) as ptot
          FROM sn_school s
            JOIN zone z USING(zone_id)
            JOIN district d ON d.district_id = z.district_id
            JOIN district p ON p.province_id = d.province_id
          GROUP BY p.province_id";
$stmt = $pdo->query($sql);
$ptot = $stmt->rowCount();
$ptot = str_pad($ptot , 3, 0, STR_PAD_LEFT);

 

 

Link to comment
Share on other sites

The number of schools is the number of rows in the school table. 

Your model implements a hierarchy:

province
  ├─ district
     ├─ zone
        ├─ school

 

To get counts of schools by Province/District and Zone, you can use GROUP BY province, district, zone WITH ROLLUP.  You should read about how WITH ROLLUP works, so you understand the "extra" rows it will produce.

SELECT p.province_id, d.district_id, z.zone_id, count(s.school_id) as countof
FROM sn_school s        
JOIN zone z USING(zone_id)
JOIN district d ON d.district_id = z.district_id
JOIN district p ON p.province_id = d.province_id
GROUP BY p.province_id, d.district_id, z.zone_id WITH ROLLUP

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.