MySQL two tables two columns COUNT -


i have 2 tables

teams - team_id , team_name (among other stuff)
schedule - game_id, team_a, team_b, team_a_id, team_a_id (among other stuff)

i trying create result find out number of times team_name (or team_id) shows in either column team_a or team_b (or team_a_id or team_b_id)

teams
team_id team_name
1001 new york
1011 cleveland
1021 detroit
1031 houston

schedule
game_id team_a team_b team_a_id team_b_id
1 new york cleveland 1001 1011
2 new york detroit 1001 1021
3 cleveland houston 1011 1031

answer: new york 2 cleveland 2 detroit 1 houston 1

try this:

select count(*) teams t join schedule s on (s.team_a = t.team_name or s.team_b = t.team_name) 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -