mpmullally Posted January 16, 2013 Share Posted January 16, 2013 (edited) I have a database with 4 columns: Location Date Team_Name Score I need the results page to SELECT AND combine "Team_Name" WHEN equal and SUM "Score" AS Total and only show the current month at an individual location "BW3Avon". Here is my code "<?php $con = mysql_connect("localhost","**************","***************"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("x0kqgoub_bw3avonscore", $con); $sql = "SELECT * FROM bw3avonscore WHERE MONTH(myDate) = MONTH(CURRENT_DATE)"; $result = mysql_query("SELECT * FROM bw3avonscore IF Team_Name = Team_Name THEN SUM(Score) AS Total ORDER BY Total DESC"); while($row = mysql_fetch_array($result)) { echo $row['Team_Name'] . " " . $row['Score']; echo "<br />"; } mysql_close($con); ?>" Edited January 16, 2013 by mpmullally Quote Link to comment https://forums.phpfreaks.com/topic/273224-results-combine-rows-by-column-1-then-sum-by-column-2-into-total-then-order-by-desc/ Share on other sites More sharing options...
requinix Posted January 16, 2013 Share Posted January 16, 2013 SELECT Team_Name, SUM(Score) AS Total FROM bw3avonscore WHERE MONTH(myDate) = MONTH(NOW()) GROUP BY Team_Name ORDER BY Total DESC Quote Link to comment https://forums.phpfreaks.com/topic/273224-results-combine-rows-by-column-1-then-sum-by-column-2-into-total-then-order-by-desc/#findComment-1406049 Share on other sites More sharing options...
mpmullally Posted January 16, 2013 Author Share Posted January 16, 2013 SELECT Team_Name, SUM(Score) AS Total FROM bw3avonscore WHERE MONTH(myDate) = MONTH(NOW()) GROUP BY Team_Name ORDER BY Total DESC This gives an empty result. I get a result with this query("SELECT * FROM bw3avonscore Team_Name Order By Score Desc") but when a team name comes up twice it won't SUM the score column Quote Link to comment https://forums.phpfreaks.com/topic/273224-results-combine-rows-by-column-1-then-sum-by-column-2-into-total-then-order-by-desc/#findComment-1406251 Share on other sites More sharing options...
requinix Posted January 17, 2013 Share Posted January 17, 2013 Try without the WHERE, and then make sure the myDate is a DATE/DATETIME column and there's at least one row from this month. And you know the query will return the total score as "Total" and not "Score", right? That's what you looked for in the original query but you're free to rename it. Quote Link to comment https://forums.phpfreaks.com/topic/273224-results-combine-rows-by-column-1-then-sum-by-column-2-into-total-then-order-by-desc/#findComment-1406258 Share on other sites More sharing options...
mpmullally Posted January 17, 2013 Author Share Posted January 17, 2013 thanks it was the Where statement. removing the second where statement gave me the results i needed. Quote Link to comment https://forums.phpfreaks.com/topic/273224-results-combine-rows-by-column-1-then-sum-by-column-2-into-total-then-order-by-desc/#findComment-1406260 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.