I have three tables.
managersTbl
ID | NAME |
1 | Ana |
2 | Elsa |
3 | Olaf |
4 | Belle|
gigsTbl
ID | GIG | EARNING | MANAGER | ARTIST |
1 | sing | 500 | 2 | 1 |
2 | act | 100 | 2 | 3 |
3 | modelling | 250 | 3 | 4 |
4 | dance | 10 | 1 | 1 |
artistsTbl
ID | NAME |
1 | Haley |
2 | Aw |
3 | Fire |
4 | Finn |
What I want to accomplish is get all the managers, and get the sum of their gigs earning.
SELECT id, name
ifnull(b.earning, 0) AS earning
FROM doctorsTbl
LEFT JOIN(
SELECT id, name
SUM(earning) AS earning
FROM gigsTbl
GROUP BY manager_id
) b on (a.id = b.manager_id)
I have already achieved getting all the managers, and the sum of their gigs earnings with the above query. Now, I want to get all the artists, without getting repeated, from the gigsTbl grouped by their manager.
I've tried using inner join like this
SELECT id, name
ifnull(b.earning, 0) AS earning
FROM doctorsTbl
LEFT JOIN(
SELECT id, name
SUM(earning) AS earning
FROM gigsTbl
INNER JOIN artistsTbl d
on b.artist = d.id
GROUP BY manager_id
) b on (a.id = b.manager_id)
I want to do this without creating a new query for the artists because I'd like to be able to search the tables with using the artist identification.