I have three tables linked each other.
mysql> select * from tablea;
+----+--------+
| id | name |
+----+--------+
| 1 | Item 1 |
| 2 | Item 2 |
+----+--------+
2 rows in set (0.00 sec)
mysql> select * from tableb;
+----+------+----------+
| id | Aid | name |
+----+------+----------+
| 1 | 1 | B Item 1 |
| 2 | 2 | B Item 2 |
| 3 | 1 | B Item 3 |
+----+------+----------+
3 rows in set (0.00 sec)
mysql> select * from tablec;
+----+------+----------+-------+
| id | Bid | name | value |
+----+------+----------+-------+
| 1 | 1 | C Item 1 | 10 |
| 2 | 2 | C Item 2 | 20 |
| 3 | 1 | C Item 3 | 15 |
| 4 | 2 | C Item 4 | 5 |
| 5 | 3 | C Item 5 | 12 |
+----+------+----------+-------+
5 rows in set (0.00 sec)
TableA is linked to TableB with Aid, TableC is linked with TableB with Bid.
What I want is the id of tableA and the sum of values of all the TableC items comes under it.
The result set I expect for the above example is
+-----------+--------+
| tablea.id | sum |
+-----------+--------+
| 1 | 37 |
| 2 | 25 |
+-----------+--------+
if there’s a possibility that
IDfrom tableA doesn’t exist on other tables and you also want to show their result, useLEFT JOINinstead