ViperSBT Posted June 23, 2006 Share Posted June 23, 2006 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 pointsFROM dads dLEFT JOIN singles s ON s.dad = d.dnumberLEFT JOIN points p ON p.dad = d.dnumberWHERE d.cat =45 && d.payment != 'pending'GROUP BY d.dnumberORDER 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. Quote Link to comment Share on other sites More sharing options...
Wildbug Posted June 23, 2006 Share Posted June 23, 2006 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? Quote Link to comment Share on other sites More sharing options...
ViperSBT Posted June 24, 2006 Author Share Posted June 24, 2006 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... Quote Link to comment Share on other sites More sharing options...
Barand Posted June 25, 2006 Share Posted June 25, 2006 Beacuse of the doubling-up effect of multiple joins you will need a subquery or, if Mysql version < 4.1, a temporary table. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.