eciym Posted September 9, 2009 Share Posted September 9, 2009 MySQL 5.0.27 hej, I was wondering if some of you wonderful people could give me a heads up on this, I have been reading up and have summised that I need a LEFT JOIN As i want it to list all the teams regardless of if they have points or not. I have a table of teams with a UID and the team name. then i have a table called points that stores the UID of the teams table as a foreign key, the number of points and each entry is timestamped for historical review later on. what I want to do is display all the teams and the total number of points each team has acrued. I get the sum of the teams with this $query = "SELECT pointsfid, SUM(teampoints) FROM points GROUP BY pointsfid"; I presumed that being a relational database i wouldnt have to save the team name in the points table. Assumed it would be possible to get the team names also. but how to get a query to display the team as well has foxed me. I got this far SELECT pointsfid, SUM(teampoints) AS totalpoints FROM points GROUP BY pointsfid LEFT JOIN teams ON points.pointsfid = teams.teid was I even close? miles out? what did I miss? This seems like it should be so simple. Am I barking up the wrong tree? should I be looking to create a totals feild in the teams table and update that with a total from the points table. to me that seems like it shouldnt be needed. another question pops in to my head also; How do I echo the alias totalpoints in PHP? Thanks in advance for any help with this. Quote Link to comment https://forums.phpfreaks.com/topic/173634-mysqlphp-formatsyntax-left-join-arrrrgghh/ Share on other sites More sharing options...
kickstart Posted September 9, 2009 Share Posted September 9, 2009 Hi You were close:- SELECT teams.teid, SUM(teampoints) AS totalpoints FROM teams LEFT JOIN points ON teams.teid = points.pointsfid GROUP BY teams.teid The alias is just treated like any other column name in php. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/173634-mysqlphp-formatsyntax-left-join-arrrrgghh/#findComment-915320 Share on other sites More sharing options...
eciym Posted September 9, 2009 Author Share Posted September 9, 2009 Thankyou very, very much Keith you have saved me from a monster migrane! Quote Link to comment https://forums.phpfreaks.com/topic/173634-mysqlphp-formatsyntax-left-join-arrrrgghh/#findComment-915713 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.