mypoints <- could be thousands of records
mycategories <- 10-20 records
myaccounts <- a few records
Simple structure of myaccounts which contains mycategories which contains mypoints.
For one particular user I would like to retrieve the summary information of points, I have a working sql query but would like to know if it is the best way of doing it. I have little understanding of joins and such.
Here is what I have so far:
SELECT a.id, a.description, SUM( p.points ) AS currentbalance, SUM( IF( p.status = 0, 0, p.points ) ) AS availablebalance, COUNT( p.points ) AS records FROM myaccounts AS a LEFT JOIN mycategories AS c ON c.acct_id = a.id LEFT JOIN mypoints AS p ON p.cat_id = c.id WHERE a.status = 1 AND c.status = 1 AND p.user_id = $user_id GROUP BY a.id ORDER BY a.orderingThis returns all account summary info if a user has points
So then I got to experimenting, the following works also but because of my lack of understanding the internal workings of mysql I was wondering which is better for the sql engine.
SELECT a.id, a.description, SUM( p.points ) AS currentbalance, SUM( IF( p.status = 0, 0, p.points ) ) AS availablebalance, COUNT( p.points ) AS records FROM mypoints AS p LEFT JOIN mycategories AS c ON c.id = p.cat_id LEFT JOIN myaccounts AS a ON a.id = c.acct_id WHERE a.status = 1 AND c.status = 1 AND p.user_id = $user_id GROUP BY a.id ORDER BY a.ordering
Is it my understanding that the first query is better? Or maybe neither is and someone has a better way of doing this.