This is complex query, and I was hoping to achieve it one statement, rather than have to juggle array values in PHP.
To achieve the desired output of:
User Jobs Total
John D. 5 $1245.67
Mary L. 3 $800.56
So far, this is the query I have:
SELECT
SUM(job.cost) AS sum,
COUNT(DISTINCT job.user) as count,
user.id, user.firstname, user.lastname
FROM `job`
LEFT JOIN `user` ON job.user = user.id
GROUP BY user.id
But the count value is wrong: it's the distinct user, so of course each is going to be wrong. How do I fix this?
TABLE user
id, name, etc.
TABLE job
id, user, cost
With ONE user TO MANY job
UPDATE
This seems to be working right:
SELECT
SUM(job.cost) AS sum,
COUNT(1) as count,
user.id, user.firstname, user.lastname
FROM `job`
LEFT JOIN `user` ON job.user = user.id
GROUP BY user.id
COUNT(DISTINCT job.id)(or any other unique column injob