#### Archived

This topic is now archived and is closed to further replies.

# JOIN GROUP help...

## Recommended Posts

OK, so it is the most complex query that I have put together to date...

The query looks like this:
[code]SELECT d.dnumber, d.cname, MIN( s.time ) AS singles, SUM( p.points ) AS points
LEFT JOIN singles s ON s.dad = d.dnumber
LEFT JOIN points p ON p.dad = d.dnumber
WHERE d.cat =45 && d.payment != 'pending'
GROUP BY d.dnumber
ORDER BY cname[/code]

The result shows all the rows I am expecting, and everything is in order except for the 'points' result. It appears that the sum of all the points entries in the points table for a particular dad are being multiplied by the number of entries that dad is showing up in the singles table. Does that make sense?

So a dad that has 100 points and is in the singles table once = 100, which would be correct.
A dad that has 100 points and in the singles table 3 times = 300, which is incorrect and should only be the 100.

##### Share on other sites

Well, if a dad is in the table three times, and the point=100 for each entry, then SUM is just doing what you asked it to, and adding up the points (which would be, and is, 300). If there are duplicate entries in your table, and the "points" value is always equal for them, then you don't need the SUM if you just want to return the points. Use "p.points" instead of "SUM(p.points)". Will that work for you?

##### Share on other sites
A dad is a single entry in the 'dads' table. A dad MAY have multiple entries in both the 'singles' and 'points' tables. So what I am wanting to do is find out what the smallest number is for a dad in the 'singles' table as well as the total number of points the dad has earned by summing all of his results from the 'points' table...

##### Share on other sites
Beacuse of the doubling-up effect of multiple joins you will need a subquery or, if Mysql version < 4.1, a temporary table.