Jump to content

JOIN GROUP help...


ViperSBT

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
FROM dads d
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.
Link to comment
https://forums.phpfreaks.com/topic/12731-join-group-help/
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?
Link to comment
https://forums.phpfreaks.com/topic/12731-join-group-help/#findComment-48810
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...
Link to comment
https://forums.phpfreaks.com/topic/12731-join-group-help/#findComment-48944
Share on other sites

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.