I have 2 tables. Problem and problem_votes. There are many rows in the problems table and just one in the votes table.
I have this query:
select
problems.problem_id,
creator_member_id,
problem_title,
problem_description,
sum( vote ) as totalVotes,
problem_date
from problems
left join problem_votes on
problems.problem_id = problem_votes.problem_id
I expected there to be returned a list of problems, but I only get one item for some reason.
If I take out the sum function, it returns twenty rows. Is there a way to get the query to get the sum of the votes for each problem?
You need a
GROUP BYotherwise it will aggregate the entire table instead of returning one row for each problem_id:This query uses a MySQL extension:
GROUP BYandHAVINGwith Hidden Columns