Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot

you need to use GROUP BY in order to split score's sum over different users, further more if you use LEFT JOIN then you'll be sure to get a row for each user instance even if it has no rows in stats table (then, using COALESCE as regileroregilero suggested, the sum will be 0)

something like:

SELECT u.id, u.username, u.email, u.active, u.admin, u.team_id, t.name AS team_name, sum( COALESCE(s.score) ) AS total_score FROM users u LEFT JOIN teams t ON u.team_id = t.id LEFT JOIN stats s ON u.id = s.user_id GROUP BY u.id, u.username, u.email, u.active, u.admin, u.team_id, t.name 

this is not tested

you need to use GROUP BY in order to split score's sum over different users, further more if you use LEFT JOIN then you'll be sure to get a row for each user instance even if it has no rows in stats table (then, using COALESCE as regilero suggested, the sum will be 0)

something like:

SELECT u.id, u.username, u.email, u.active, u.admin, u.team_id, t.name AS team_name, sum( COALESCE(s.score) ) AS total_score FROM users u LEFT JOIN teams t ON u.team_id = t.id LEFT JOIN stats s ON u.id = s.user_id GROUP BY u.id, u.username, u.email, u.active, u.admin, u.team_id, t.name 

this is not tested

you need to use GROUP BY in order to split score's sum over different users, further more if you use LEFT JOIN then you'll be sure to get a row for each user instance even if it has no rows in stats table (then, using COALESCE as regilero suggested, the sum will be 0)

something like:

SELECT u.id, u.username, u.email, u.active, u.admin, u.team_id, t.name AS team_name, sum( COALESCE(s.score) ) AS total_score FROM users u LEFT JOIN teams t ON u.team_id = t.id LEFT JOIN stats s ON u.id = s.user_id GROUP BY u.id, u.username, u.email, u.active, u.admin, u.team_id, t.name 

this is not tested

added 113 characters in body
Source Link
Dalen
  • 9k
  • 4
  • 49
  • 53

you need to use GROUP BY in order to split score's sum over different users, further more if you use LEFT JOIN then you'll be sure to get a row for each user instance even if it has no rows in stats table (then, using COALESCE as regilero suggested, the sum will be 0)

something like:

SELECT u.id, u.username, u.email, u.active, u.admin, u.team_id, t.name AS team_name, sum( COALESCE(s.score) ) AS total_score FROM users u LEFT JOIN teams t ON u.team_id = t.id LEFT JOIN stats s ON u.id = s.user_id GROUP BY u.id, u.username, u.email, u.active, u.admin, u.team_id, t.name 

this is not tested

you need to use GROUP BY in order to split score's sum over different users, further more if you use LEFT JOIN then you'll be sure to get a row for each user instance even if it has no rows in stats table (then the sum will be 0)

something like:

SELECT u.id, u.username, u.email, u.active, u.admin, u.team_id, t.name AS team_name, sum( s.score ) AS total_score FROM users u LEFT JOIN teams t ON u.team_id = t.id LEFT JOIN stats s ON u.id = s.user_id GROUP BY u.id, u.username, u.email, u.active, u.admin, u.team_id, t.name 

this is not tested

you need to use GROUP BY in order to split score's sum over different users, further more if you use LEFT JOIN then you'll be sure to get a row for each user instance even if it has no rows in stats table (then, using COALESCE as regilero suggested, the sum will be 0)

something like:

SELECT u.id, u.username, u.email, u.active, u.admin, u.team_id, t.name AS team_name, sum( COALESCE(s.score) ) AS total_score FROM users u LEFT JOIN teams t ON u.team_id = t.id LEFT JOIN stats s ON u.id = s.user_id GROUP BY u.id, u.username, u.email, u.active, u.admin, u.team_id, t.name 

this is not tested

deleted 2 characters in body
Source Link
Dalen
  • 9k
  • 4
  • 49
  • 53

you need to use GROUP BY in order to split score's sum over different users, further more if you use LEFT JOIN then you'll be sure to get a row for each user instance even if it has no rows in stats table (then the sum will be 0)

something like:

SELECT u.id, u.username, u.email, u.active, u.admin, u.team_id, t.name AS team_name, sum( s.score ) AS total_score FROM users u INNERLEFT JOIN teams t ON u.team_id = t.id INNERLEFT JOIN stats s ON u.id = s.user_id GROUP BY u.id, u.username, u.email, u.active, u.admin, u.team_id, t.name 

this is not tested

you need to use GROUP BY in order to split score's sum over different users

something like:

SELECT u.id, u.username, u.email, u.active, u.admin, u.team_id, t.name AS team_name, sum( s.score ) AS total_score FROM users u INNER JOIN teams t ON u.team_id = t.id INNER JOIN stats s ON u.id = s.user_id GROUP BY u.id, u.username, u.email, u.active, u.admin, u.team_id, t.name 

this is not tested

you need to use GROUP BY in order to split score's sum over different users, further more if you use LEFT JOIN then you'll be sure to get a row for each user instance even if it has no rows in stats table (then the sum will be 0)

something like:

SELECT u.id, u.username, u.email, u.active, u.admin, u.team_id, t.name AS team_name, sum( s.score ) AS total_score FROM users u LEFT JOIN teams t ON u.team_id = t.id LEFT JOIN stats s ON u.id = s.user_id GROUP BY u.id, u.username, u.email, u.active, u.admin, u.team_id, t.name 

this is not tested

Source Link
Dalen
  • 9k
  • 4
  • 49
  • 53
Loading