I'm trying to lookup some totals by state in these 3 tables. We have a league table, which contains the state column, then we have a team table which has a reference back to league, then we have a member table which references back to the team table. I'm trying to figure out how to get totals on all teams in each state and all members in each state with only one query.
The closest I can get is:
I just don't think my numbers are right because when I remove the member table from the query my team totals are different. What am I missing here?
The closest I can get is:
Code:
SELECT `l`.`state` AS `state`, COUNT(`t`.`team_id`) AS `teams`, COUNT(`m`.`mem_id`) AS `members`
FROM `leagues` AS `l`
INNER JOIN `teams` AS `t` ON `s`.`league_id` = `t`.`team_league_id`
LEFT OUTER JOIN `members` AS `m` ON `t`.`team_id` = `m`.`mem_team_id`
WHERE LENGTH(`l`.`state`) > 0
GROUP BY `l`.`state`
ORDER BY `l`.`state` ASC
I just don't think my numbers are right because when I remove the member table from the query my team totals are different. What am I missing here?