How can I display information from 4 different tables as one table? I have 4 tables that have related information. These tables contain packages, organisation children and system users data. I want to query what type of package an organisation has subscribed to and how many children and users are registered under that organisation. The sample data contained on the tables is as shown below.
Packages Table
|-------------------------------|
| package_id | package_name |
--------------------------------|
| 12 | Basic |
| 21 | Pro |
| 33 | Premium |
---------------------------------
Organisations Table
|-------------------------------------------------------|
| org_id | org_name | package_id |
|-------------------------------------------------------|
| 18 | Marks of Awesomeness | 12 |
| 24 | John Hopkins Hospital | 21 |
| 38 | Teddy and the Wailers | 33 |
| 78 | Lawrence Movers | 12 |
|--------------------------------------------------------
Children's Table
|------------------------------------------------|
| id_child | id_org child_name | id_org |
|------------------------------------------------|
| 14 | Mark Walker | 18 |
| 22 | Jane Quinn | 24 |
| 38 | Lily Audrey | 24 |
| 44 | Dona Marie | 18 |
|-------------------------------------------------
Users Table
|------------------------------------------------|
|idu | org_id | fname | lname |
|------------------------------------------------|
|87 | 18 | John | Doe |
|92 | 33 | Jane | Doe |
|107 | 18 | Martin | Short |
|112 | 18 | Jason | Seguel |
|127 | 33 | Josh | Radnor |
|-------------------------------------------------
My query is as shown below
SELECT SQL_CALC_FOUND_ROWS `id_org`, `org_name`, `package_name`
,COUNT(id_child) AS child_count, COUNT(idu) AS user_count FROM organisations,packages,children,system_users
WHERE organisations.id_org=children.org_id AND organisations.id_org=system_users.org_id
AND organisations.org_package_id=packages.id_package
The problem with the query is that it only shows an organisation only if it has a child and a user listed using its org_id. I want to list all the data for all organisations with the package name it has subscribed to, total number of children listed under that particular organisation, total number of users listed under that particular organisation and zero for where an organisation has no child, a user or both. Below are my desired results. Thanks
Desired Results
|------------------------------------------------------------------------------------|
| Organisation | package Name | No of Children | Number of Users |
|------------------------------------------------------------------------------------|
| Marks of Awesomeness | Basic | 2 | 3 |
| John Hopkins Hospital | Pro | 2 | 0 |
| Teddy and the Wailers | Premium | 0 | 2 |
| Lawrence Movers | Basic | 0 | 0 |
--------------------------------------------------------------------------------------