0

I have two tables: teams(id, name) and users(id, name, team_id)

I want a following result:

Team  | Members 
team1 | 10 
team2 | 14 
team3 | 8

I tried:

SELECT t.name AS 'Team', COUNT(u.email) AS 'Members' FROM teams t INNER JOIN users u ON (u.team_id = t.id) 

but it wouldn't work.

2 Answers 2

5

You should use GROUP BY, and you have a syntax error for alias, try this:

SELECT t.name AS `Team`, COUNT(u.email) AS `Members`
FROM teams t INNER JOIN users u ON (u.team_id = t.id)
GROUP BY t.name -- or t.id
Sign up to request clarification or add additional context in comments.

Comments

0

You need to GROUP BY the teams name value:

SELECT t.name AS `Team`, 
       COUNT(u.email) AS `Members` 
FROM teams t 
INNER JOIN users u ON u.team_id = t.id
GROUP BY t.name

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.