Jump to content

Recommended Posts

I have a table containing player_id, goals and assists.

 

player id | goals | assists|
-------------------------
1           |  10   |        11
2           |  0     |        1
3           |  4     |        4
4           |  2     |        0
------------------------

 

What I want to do is pull the data from the database, get the sum of goals and assists as points for each row and order by points descending.

 

I have the following code

$sql = "SELECT player_id, player_number, forename, surname, games_played, goals, assists
  FROM ww_players
  LIMIT 0,9";

$result = mysql_query($sql) or die (mysql_error());
$i=0;
while ($row = mysql_fetch_assoc($result)) {
	$data[] = $row;

	$i++;
}

 

I can add the two values when outputting the data but I want to be able to order by the total before doing this.

 

How do I go about doing it?

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/158634-solved-value-of-two-field-php-mysql/
Share on other sites

$sql = "SELECT player_id, player_number, forename, surname, games_played, goals, assists, SUM(goals + assists) as points
  FROM ww_players
  ORDER BY points DESC
  LIMIT 0,9";

 

If the order by does not work, try this:

  ORDER BY SUM(goals + assists) DESC

 

If it requires a GROUP BY, try this for that:

 

  GROUP BY player_id, player_number, forename, surname, games_played

 

Sorry my SQL is shabby. Moving the the MySQL forum.

$sql = "SELECT player_id, player_number, forename, surname, games_played, goals, assists, SUM(goals + assists) as points
	  FROM ww_players
	  GROUP BY player_id
	  ORDER BY points DESC, goals DESC
	  LIMIT 0,10";

 

 

Cheers for your help.

 

Solved with the code above.

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.