Jump to content

Results Combine Rows by Column 1 Then SUM by Column 2 into Total THEN Order by DESC


Recommended Posts

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 by mpmullally

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

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.